Kohana - passing variables to a view
-
31-05-2021 - |
Question
I'm not able to pass a simple variable to the view , can someone tell me what I'm doing wrong ?
I have a controller user :
class Controller_User extends Controller_Template_Login {
public function action_index()
{
$this->template = 'user/info';
parent::before();
$user = Auth::instance()->get_user();
$this->template->content = View::factory('user/info')
->bind('user', $user)
->bind('message', $message)
->bind('errors', $errors);
// if a user is not logged in, redirect to login page
if (!$user)
{
Request::current()->redirect('user/login');
}
}
}
Inside my view (user/info) i get that user is not defined .
what I'm missing ?
Edit : Note that this can be fixed by adding the :
$user = Auth::instance()->get_user();
to the Controller_Template_Login
$user = Auth::instance()->get_user();
$this->template->user = $user;
What i want to know is why this doesn't work :
$user = Auth::instance()->get_user();
$this->template->content = View::factory('user/info')
->bind('user', $user)
->bind('message', $message)
->bind('errors', $errors);
Solution
Because in the first case
$this->template->user = $user;
you assign $user
variable to a user
property of $this->template
.
And in the second case:
$this->template->content = View::factory('user/info')
->bind('user', $user)
->bind('message', $message)
->bind('errors', $errors);
you bind $user
variable to a user
property of $this->template->content
.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow