0

我的控制器动作需要一个参数,但我无法让 KO3 的路由器在默认路由中传递此参数。这种事情适用于其他路线。这是一个澄清的例子......

在 bootstrap.php 中...

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

在控制器文件中...

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

2 回答 2

1

获取指定参数的另一种方法是:

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

您还应该确保按顺序定义路线,并确保它符合预期。

于 2010-05-16T21:35:18.060 回答
0

问题是由贪婪路由引起的(将匹配任何 uri),因此路由器从未到达默认路由。下面是一个供参考的例子...

// 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',
    ));
于 2010-05-13T19:53:45.473 回答