tags:

views:

277

answers:

5

Is there a mechanism to pass extra parameters to Controller actions in Kohana?

Eg:

$config['article/([0-9]+)'] = array('path' => 'news/show/$1', 
                                    'params' => array(
                                       'param1' => 'some_stuff',
                                    ));

The Kohana routing documentation doesn't seem to discuss this. But, is there a way to get this working.

A: 

I think you can go this way to whatever level you like:

$config['article/([0-9]+)'] = 'news/show/more/more/more/etc/$1';
Sarfraz
A: 

Or maybe if you want the extra parameters to be $_GETable variables, then:

$config['article/([0-9]+)'] = 'news/show/$1?param1=some_stuff&param2=another_stuff';
Lukman
A: 

Routes rewrite URLs, so you can include any data you want in the new URL, but not in the way you suggest. The key is to understand that you can only enter a URL.

This is one way to do it:

$config['article/([0-9]+)'] = 'news/show/$1/some_stuff';

Then you can catch the arguments in the News controller's show method.

See also the Kohana documentation on Controller with arguments.

If you need something fancier, you can create more complex URLs or serialize and urlencode data in the URL.

Christian Davén
A: 

it happens automatically, you don't need to do anything in the routing config, just do this

class Controller_News extends Controller {
     public function action_show($param1, $param2 = "can have default value too"){
          // can be called with 'yoursite.com/news/show/param1/param2'
     }
}
antpaw
A: 

This has changed in Kohana 3 you have to change your bootstrap.php file to make this work.

from the unofficial wiki

http://kerkness.ca/wiki/doku.php?id=routing:routing_basics

Basic Route with 2 parameters

Unlike KO2, KO3 Routing default in your bootstrap does not handle 2 or more parameters like example.com/<controller>/<action>/<param1>/<param2>

In you bootsrtap.php file...

  Route::set('default', '(<controller>(/<action>(/<id1>(/<id2>))))')
jfountain