Kohana 会话问题

发布于 2024-12-22 21:33:37 字数 2490 浏览 0 评论 0原文

让我尝试解释一下我想在这里做什么。我正在尝试将一个宠物项目从 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(); 的易用性。设置后可以在任何控制器、模型和视图中调用的函数。

预先感谢您对我的错误行为提出的任何意见。

Let me try to explain what I want to do here. I am trying to re-write a pet project from Codeigniter 2.x to Kohana 3.2.x.

I have created a Site Template controller (below)

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();
      }

After the login form is submitted I set the session data and I am able to retrieve the session data in the Controllers that extend the Controller_Site_Template but I am unable to retrieve the session data in any of the View files.

The only way I am able to get the session data in the view files is to pass the session data in each controller that extends the Template_Site_Template:

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

Is there an easy way to establish and set the session in the template_controller that can be used in all of the controllers, modelc, views rather that using the set_global on each individual controller?

I don't know if I am explaining this well but I am used to the ease of Codeigniter's $this->session->userdata(); function that can be called in any controller, model, and view once it was set.

Thank you in advance for any input on what I am doing incorrectly.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

养猫人 2024-12-29 21:33:37

您可以使用以下命令将全局数据设置或绑定到您的视图。

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

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

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

编辑:哦,以上仅适用于视图,您希望它遍及整个应用程序。

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

You can set or bind global data to your views with the following

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

If you plan to change any data further along the application logic, then use bind.

If no more changes to the data are required, use set.

Edit: oh, the above is just for views and you want it across the entire application.

Just use the Session::instance()->set() and Session::instance()->get() as required across your application rather then assigning it in your application controller.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文