Search code examples
kohanakohana-3kohana-3.3

getting controller route parameter


Using Kohana 3.3, I created a tabbed interface and I'm trying to detect which tab is active based on a route parameter.

Testing with 2 urls which look like this: mysite.com/p/mycontroll and: mysite.com/p/Francis-Lewis/mycontroll

My route looks like this:

Route::set('profile', 'p(/<name>)(/<controller>(/<action>))', array(
        'name'          => '[\w\-]+',
        'controller'    => '[a-z]+',
        'action'        => '(view|edit|save|delete|create|cancel)',
    ))->defaults(array(
        'name'          => null,
        'directory'     => 'profile',
        'controller'    => 'main',
        'action'        => 'index',
    )); 

The route itself is working fine, selecting the mycontroll controller. Here's where the problem comes in. In the controller:

$this->request->param('controller'); // returns NULL

In the view

<?= Request::current()->param('controller') ?> // returns NULL

After banging my head around for a while, I added a function to the Kohana Request class to return the $_params array to see what was in there.

Here's all it returns:

name => 'Francis Lewis'

Any ideas how to get the current controller?


Solution

  • There is a function for that in in the request object:

    $this->request->controller(); // Returns the current controller as a String