【问题标题】:How can i define global variables in slim framework我如何在苗条框架中定义全局变量
【发布时间】:2019-06-23 13:01:53
【问题描述】:

我如何定义一个全局变量,以便我的 current_user 方法可以工作,如果我想要它,我只需要检查是否有当前用户我的示例代码在下面

    if (isset($_SESSION['company_id'])) {
       $current_user = Companies::find($_SESSION['company_id']);
     }
    else
   {
    $current_company = null;
   }

如何在我想要的任何地方使用当前用户方法,而不像在我的 header.html 中那样将它传递给我的 app->render() 方法

{% if current_user %}
 hi {{current_user.name}}
{% endif %}

【问题讨论】:

  • 也许你可以输入:global $current_user;在你的功能中?

标签: php slim


【解决方案1】:

您可以将值注入到应用对象中:

$app->foo = 'bar';

更多关于Slim’s documentation

【讨论】:

    【解决方案2】:

    回调函数中的注入不起作用。

    要访问回调函数中的变量,您可以使用"use() " 函数:

    $mydata =  array (  ... );
    
    $app->get('/api', function(Request $request, Response $response) use($mydata) {  
            echo json_encode($mydata);
    
    });
    

    【讨论】:

      【解决方案3】:

      像这样注入对象:

      $app->companies = new Companies();
      

      如果您想确保每次都相同,也可以将其作为单例注入:

      $app->container->singleton('companies', function (){
          return new Companies();
      });
      

      这样称呼它:

      $app->companies->find($_SESSION['company_id']);
      

      更新文档链接: Slim Dependency Injection

      【讨论】:

        【解决方案4】:

        接受的答案不适用于 Slim 3(因为钩子已被移除)。

        如果您尝试为所有视图定义一个变量并且您使用的是PhpRenderer,您可以按照他们的示例进行操作:

        // via the constructor
        $templateVariables = [
            "title" => "Title"
        ];
        $phpView = new PhpRenderer("./path/to/templates", $templateVariables);
        
        // or setter
        $phpView->setAttributes($templateVariables);
        
        // or individually
        $phpView->addAttribute($key, $value);
        

        【讨论】:

          【解决方案5】:

          我终于可以让它工作了

             $app->hook('slim.before.dispatch', function() use ($app) { 
                 $current_company = null;
                 if (isset($_SESSION['company_id'])) {
                    $current_company = Company::find($_SESSION['company_id']);
                 }
                 $app->view()->setData('current_company', $current_company);
              });
          

          【讨论】:

            【解决方案6】:

            带树枝/视图

            创建中间件

            <?php
            
            namespace ETA\Middleware;
            
            class GlobalVariableMiddleware extends Middleware {
            
                public function __invoke($request, $response, $next) {
            
                    $current_path = $request->getUri()->getPath();
            
                    $this->container->view->getEnvironment()->addGlobal('current_path', $current_path);
            
                    return $next($request, $response);
                }
            
            }
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2019-03-20
              • 2018-01-27
              • 2016-05-07
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多