Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kohana 3 get current controller/action/arguments

In Kohana 2 you could easily get that information like this:

echo router::$controller;
echo router::$method;
echo router::$arguments[0-x];

Any idea how that works in Kohana 3?

Thanks in advance!

like image 625
n00b Avatar asked May 04 '10 08:05

n00b


2 Answers

From inside a controller:

$this->request->controller

$this->request->action

$this->request->param('paramname')

Unlike K2 arguments in K3 are accessed via kays which you define in your routes.

Take for example this url:

Route::set('default', '(<controller>(/<action>(/<id>)))')    
    ->defaults(array('controller' => 'welcome', 'action' => 'index')); 

To access the "id" argument you'd call

$this->request->param('id')

You can't access the controller / action arguments from the param() method.

Note, you can also use Request::instance() to get the global (or "master") request instance.

For more information see the K3 guide

like image 63
Matt Avatar answered Oct 18 '22 16:10

Matt


Updated answer for Kohana 3.2, from the user guide:

// From within a controller:
$this->request->action();
$this->request->controller();
$this->request->directory();

// Can be used anywhere:
Request::current()->action();
Request::current()->controller();
Request::current()->directory();
like image 44
Yarin Avatar answered Oct 18 '22 16:10

Yarin