【问题标题】:Kohana Sessions IssueKohana Sessions 问题
【发布时间】:2011-12-24 16:00:04
【问题描述】:

让我试着解释一下我想在这里做什么。我正在尝试将一个宠物项目从 Codeigniter 2.x 重写为 Kohana 3.2.x。

我创建了一个站点模板控制器(如下)

class Controller_Site_Template extends Controller_Template 
  {

      public $template      = 'templates/hero';

      /**
       * The before() method is called before your controller action.
       * In our template controller we override this method so that we can
       * set up default values. These variables are then available to our
       * controllers if they need to be modified.
       */
      public function before()
      {
          parent::before();

        if ($this->auto_render)
        {
            // Initialize empty values
            $this->template->title   = '';
            $this->template->content = '';
            $this->template->session = '';

            $this->template->styles = array();
            $this->template->footer_scripts = array();

          $session = Session::instance();
          $this->template->session = $session;

        }

      }

      /**
       * The after() method is called after your controller action.
       * In our template controller we override this method so that we can
       * make any last minute modifications to the template before anything
       * is rendered.
       */
      public function after()
      {
            if ($this->auto_render)
            {
                $styles = array(
                    'assets/css/style.css' => 'screen',);


                $footer_scripts = array(
                                    'assets/js/libs/jquery-1.7.1.min.js',
                    'assets/js/application.js',
                );

                $this->template->styles = array_merge( $this->template->styles, $styles );
                $this->template->footer_scripts = array_merge( $this->template->footer_scripts, $footer_scripts );
            }

            parent::after();
      }

提交登录表单后,我设置了会话数据,并且能够在扩展 Controller_Site_Template 的控制器中检索会话数据,但我无法在任何视图文件中检索会话数据。

我能够在视图文件中获取会话数据的唯一方法是在扩展 Template_Site_Template 的每个控制器中传递会话数据:

$this->template->content->set_global('session',$this->template->session->as_array());

是否有一种简单的方法可以在 template_controller 中建立和设置会话,该会话可用于所有控制器、modelc、视图,而不是在每个单独的控制器上使用 set_global?

我不知道我是否解释得很好,但我已经习惯了 Codeigniter 的 $this->session->userdata();设置后可以在任何控制器、模型和视图中调用的函数。

提前感谢您对我做错的任何意见。

【问题讨论】:

    标签: php session kohana kohana-3


    【解决方案1】:

    您可以使用以下方法设置或绑定全局数据到您的视图

    View::bind_global('session', $session);
    View::set_global('session', $session);
    

    如果您打算进一步更改应用程序逻辑中的任何数据,请使用 bind

    如果不需要对数据进行更多更改,请使用 set

    编辑:哦,以上只是用于视图,您希望在整个应用程序中使用它。

    只需在应用程序中根据需要使用 Session::instance()->set() 和 Session::instance()->get(),而不是在应用程序控制器中分配它。

    【讨论】:

    • 我只是习惯了 Codeigniter,而 Kohana 3 对整个 MVC 的要求更加严格。谢谢法迪。
    猜你喜欢
    • 2011-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-26
    • 2020-10-02
    • 2011-03-24
    相关资源
    最近更新 更多