views:

342

answers:

2

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;
    }
}
A: 

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',
    ));
John Himmelman
+1  A: 

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.

Zahymaka