Request :: $ controller in Cohan

Why can't I query the name of the controller in use in 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?

I need this for this:

<?php if (Request::$controller != 'index') { ?> <a href="/">Example.com</a> <?php } else { ?> Example.com <?php } ?>

+3
source share
5 answers

Do this instead on the controller:

View::bind_global('controller', $this->request->param('controller'));

Then you can access $controllerin any view.

+4
source

The request must be accessed by static methods; there is no need to define additional static properties || global vars view to get it.

Request::instance() ( " " ). Request::current() , , $this->request .

<? if (Request::current()->controller !== 'index') : ?> 

    <a href="<?= URL::site() ?>">Example.com</a> 

<? else : ?> 

    Example.com 

<?  endif; ?>
+3

, yoda, , , .

, ?

$link = (Request::$controller != 'index') ? '<a href="/">Example.com</a>' : 'Home';
$this->template->set_global('homeLink', $link);

Remember also that you can create links from your routes using Route::get()either one of your friends.

+1
source

In Kohana 3.1

<? if (Request::current()->controller !== 'index') : ?>

provide the property ErrorException [Notice]: Undefined: Request :: $ controller ". then I just use Request :: current () → controller () to view acceptable / best practice / best performance?

<? if (Request::current()->controller() !== 'index') : ?> 

    <a href="<?= URL::site() ?>">Example.com</a> 

<? else : ?> 

    Example.com 

<?  endif; ?>
0
source

Kohana 3.2: In the controller, paste this (I find it really stupid that you cannot use bind_global)

View::set_global('controller', $this->request->current()->controller());

Then in the view you can use:

echo ( $controller );
0
source

Source: https://habr.com/ru/post/1789629/


All Articles