首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Kohana Sessions问题

Kohana Sessions问题
EN

Stack Overflow用户
提问于 2011-12-25 00:00:04
回答 1查看 852关注 0票数 0

让我试着解释一下我想在这里做什么。我正在尝试从Codeigniter 2.x到Kohana 3.2.x重写一个自己喜欢的项目。

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

代码语言:javascript
复制
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的每个控制器中传递会话数据:

代码语言:javascript
复制
$this->template->content->set_global('session',$this->template->session->as_array());

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

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

提前感谢您对我所做的错误的任何输入。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-01-07 12:55:45

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

代码语言:javascript
复制
View::bind_global('session', $session);
View::set_global('session', $session);

如果您计划沿着应用程序逻辑进一步更改任何数据,那么可以使用bind

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

编辑:哦,上面的代码只用于视图,你想让它覆盖整个应用程序。

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8625510

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档