【问题标题】:Set a variable in the app_controller and use it in a CakePHP layout在 app_controller 中设置一个变量并在 CakePHP 布局中使用它
【发布时间】:2011-01-05 10:29:54
【问题描述】:

我需要在 CakePHP 的 app_controller 中设置一个变量,然后在我的默认布局文件中使用它。

有没有办法设置这个变量?

【问题讨论】:

    标签: cakephp controller


    【解决方案1】:

    我认为他的意思是,他不知道在哪里设置变量,因为他不在控制器内的特定函数中。要让变量(或其他任何东西)随处可用,你必须像这样把它放在你的 AppController 中:

    function beforeFilter()
      {
      $this->set('whatever', $whatever);
      }
    

    更多关于这些回调函数here

    【讨论】:

      【解决方案2】:

      AppController 中的回调函数是 $this->set() 变量的位置,您希望这些变量可用于所有视图和布局。 beforeFilter() 在所有控制器操作之前调用。如果要在操作运行后设置视图变量,请使用 beforeRender()。您可以访问 $this->viewVars 关联数组中的其他视图变量。

      function beforeRender() {
          $new = "Universal " . $this->viewVars['layoutTitle']; 
          $this->set('universalTitle', $new);
      }
      

      【讨论】:

        【解决方案3】:

        您可以使用它来设置变量: $this->set(compact('currentJobId'));

        它会在设置数组值的同时设置数组。

        【讨论】:

          猜你喜欢
          • 2013-04-21
          • 1970-01-01
          • 2015-06-19
          • 1970-01-01
          • 2012-11-28
          • 1970-01-01
          • 1970-01-01
          • 2011-06-19
          • 2014-01-07
          相关资源
          最近更新 更多