开发者

Kohana 3 - How do I make the Default Route pass arguments to the Controller's Action?

开发者 https://www.devze.com 2022-12-30 18:54 出处:网络
My controller action requires a parameter, but I can\'t get KO3\'s router to pass this parameter in the Default route. This sort of thing works with other routes. Here is an example to clarify...

My controller action requires a parameter, but I can't get KO3's router to pass this parameter in the Default route. This sort of thing works with other routes. Here is an example to clarify...

In bootstrap.php...

Route::set('default', '(<controller>(/<action>(/<the_required_param>)))')
 ->defaults(array(
  'controller' => 'DefaultController',
  'action'     => 开发者_开发问答'index',
  'the_required_param' => 'some_default_value',
 ));

In controller file...

class Controller_DefaultController extends Controller
{
    public function action_index($the_required_param)
    {
        echo 'value: ' . $the_required_param;
    }
}


Another way to get the specified param would be:

$this->request->param('the_required_param');

You should also ensure you define your routes in order and ensure it matches what it's supposed to.


The problem was being caused by a greedy route (would match any uri), so the Router never reached the Default route. Below is an example for reference...

// The parenthesis caused this route to match any uri
Route::set('route-4-params', '(<controller>/<action>/<p1>/<p2>/<p3>/<p4>)');

Route::set('default', '(<controller>(/<action>))')
    ->defaults(array(
        'controller' => 'default_controller',
        'action'     => 'index',
        'the_required_param'     => 'somevalue',
    ));
0

精彩评论

暂无评论...
验证码 换一张
取 消