代码之家  ›  专栏  ›  技术社区  ›  SneakyShrike

PHP Slim 3-在Slim路由中访问类对象实例

  •  2
  • SneakyShrike  · 技术社区  · 6 年前

    所以我正在学习如何编写一个slim3php身份验证应用程序,我正在使用一个示例代码结构开始。示例代码中有一个名为dependencies.php的文件,该文件包含一系列创建其他类的对象实例的函数。然后将它们分配给一个$container变量,并为每个函数指定一个名称。dependencies.php文件中的函数示例如下:

    $container['view'] = function ($container) {
        $view = new \Slim\Views\Twig(
            $container['settings']['view']['template_path'],
            $container['settings']['view']['twig'],
            [
                'debug' => true // This line should enable debug mode
            ]
        );
    
        $basePath = rtrim(str_ireplace('index.php', '', $container['request']->getUri()->getBasePath()), '/');
        $view->addExtension(new Slim\Views\TwigExtension($container['router'], $basePath));
    
        $view->addExtension(new \Twig_Extension_Debug());
    
        return $view;
    };
    
    $container['validate_sanitize'] = function ($container)
    {
        $class_path = $container->get('settings')['class_path'];
        require $class_path . 'ValidateSanitize.php';
        $validator = new ValidateSanitize();
        return $validator;
    };
    
    $container['hash_password'] = function($container)
    {
        $class_path = $container->get('settings')['class_path'];
        require $class_path . 'HashPassword.php';
        $hash = new HashPassword();
        return $hash;
    };
    

    然后在我的slim routes中以某种方式调用这些函数。例如在我的 register.php validate_sanitize

    $this->get(‘validate_sanitize’); 
    

    并赋给一个变量,然后我可以使用该变量从 验证\u消毒 班级。

    然而我不明白的是这是怎么回事 get 方法用于从dependencies.php文件调用类对象。

    $app->post('/register', function(Request $request, Response $response)
    {
        $arr_tainted_params = $request->getParsedBody();
    
        $sanitizer_validator = $this->get('validate_sanitize'); //here for example
        $password_hasher = $this->get('hash_password');
    
        $tainted_email = $arr_tainted_params['email'];
        $tainted_username = $arr_tainted_params['username'];
        $tainted_password = $arr_tainted_params['password'];
    
        $model = $this->get('model');
        $sql_wrapper = $this->get('sql_wrapper');
        $sql_queries = $this->get('sql_queries');
        $db_handle = $this->get('dbase');
    
        $cleaned_email = $sanitizer_validator->sanitize_input($tainted_email, FILTER_SANITIZE_EMAIL);
        $cleaned_username = $sanitizer_validator->sanitize_input($tainted_username, FILTER_SANITIZE_STRING);
        $cleaned_password = $sanitizer_validator->sanitize_input($tainted_password, FILTER_SANITIZE_STRING);
     });
    

    我的所有路由都包含在routes.php文件中,该文件如下所示:

     <?php
    
    require 'routes/change_password.php';
    require 'routes/forgot_password.php';
    require 'routes/homepage.php';
    require 'routes/login.php';
    require 'routes/logout.php';
    require 'routes/register.php';
    

    还有一个引导文件,它创建了一个新的Slim容器Slim App instance,还包括必要的文件。我也不完全确定什么是超薄容器或者它是做什么的。此引导文件如下所示:

    <?php
    
    session_start();
    
    require __DIR__ . '/../vendor/autoload.php';
    
    $settings = require __DIR__ . '/app/settings.php'; //an array of options containing database configurations and the path to twig templates
    
    $container = new \Slim\Container($settings); //not sure what this does
    
    require __DIR__ . '/app/dependencies.php';
    
    $app = new \Slim\App($container);
    
    require __DIR__ . '/app/routes.php';
    
    $app→run();
    

    谢谢。

    1 回复  |  直到 6 年前
        1
  •  1
  •   Zamrony P. Juhara    4 年前

    内部路由可调用, $this $container 实例。

    map() 方法 Slim\App 类,您将看到以下代码:

    if ($callable instanceof Closure) {
       $callable = $callable->bindTo($this->container);
    }
    

    bindTo() 是什么使您可以使用 $这个 变量。

    <?php 
    namespace App\Controller;
    
    class MyPostRegisterController
    {
        private $container;
    
        public function __constructor($container)
        {
            $this->container = $container;
        }
    
        public function __invoke(Request $request, Response $response)
        {
            $sanitizer_validator = $this->container->get('validate_sanitize'); 
            //do something
        }
    }
    

    然后您可以如下定义路由

    $app->post('/register', App\Controller\MyPostRegisterController::class);
    

    MyPostController 类,它尝试创建它们并传递容器实例。

    若要调用其他方法,请在用冒号分隔的类名之后附加方法名。例如,以下 route registration 将调用方法 home()

    $app->post('/register', App\Controller\MyPostRegisterController::class . ':home');