Request::$controller in Kohana
Why I cannot request a used controller's name from the View?
For example, someview.php contains:
<?php
echo Request::$controller;
?>
Kohana shows the error: “ErrorException [ Fatal Error ]: Access to undeclared static property: Request::$controller”
Why? What's wrong?
It is needed for me for doing this:
<?php if (Request::$controller != 'index') { ?>
<a href="/">Example.com</a>
<开发者_如何学编程;?php } else { ?>
Example.com
<?php } ?>
Do this instead, on the controller :
View::bind_global('controller', $this->request->param('controller'));
Then you can access $controller
on any view.
Request should be accessed by it's static methods, there's no need to define additional static properties || global view vars to get it.
Request::instance()
will return the the main request instance ('mother instance').
Request::current()
will return the instance of the currently active request, the same thing you can access with $this->request
in Controller.
<? if (Request::current()->controller !== 'index') : ?>
<a href="<?= URL::site() ?>">Example.com</a>
<? else : ?>
Example.com
<? endif; ?>
I would do as yoda suggested, however I would probably put that logic in the controller as well.
I assume you want a link back to home?
$link = (Request::$controller != 'index') ? '<a href="/">Example.com</a>' : 'Home';
$this->template->set_global('homeLink', $link);
Don't forget too you can build links from your routes by using Route::get()
or one of its friends.
In Kohana 3.1
<? if (Request::current()->controller !== 'index') : ?>
give the "ErrorException [ Notice ]: Undefined property: Request::$controller". then i simply using Request::current()->controller() in view acceptable/ best practice/ optimal performance?
<? if (Request::current()->controller() !== 'index') : ?>
<a href="<?= URL::site() ?>">Example.com</a>
<? else : ?>
Example.com
<? endif; ?>
Kohana 3.2: In the controller, paste this (I find it really really stupid that you can't use bind_global)
View::set_global('controller', $this->request->current()->controller());
Then in the view, you can use:
echo ( $controller );
精彩评论