Reputation: 116
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.
Upvotes: 0
Views: 860
Reputation: 113
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.
Upvotes: 1