Routing Kohana 3.2路由重写操作
我有一个类似于Routing Kohana 3.2路由重写操作,routing,kohana,rewrite,Routing,Kohana,Rewrite,我有一个类似于/的uri方案,它从一个旧站点继承而来,我正在将该站点移植到Kohana framewerk。可能的URL是http://www.example.com/us/1234_1/foo 的值是0到9之间的数字:\d。每个数字都表示一个动作,例如0isoverview 在正确移植请求的操作的情况下,如何在引导中设置路由 这就是我现在所拥有的,但显然它的行为不正确,因为它试图调用函数Action\u 0,而不是Action\u Overview,给定示例url: Route::set('d
/
的uri方案,它从一个旧站点继承而来,我正在将该站点移植到Kohana framewerk。可能的URL是http://www.example.com/us/1234_1/foo
的值是0到9之间的数字:\d
。每个数字都表示一个动作,例如0
isoverview
在正确移植请求的操作的情况下,如何在引导中设置路由
这就是我现在所拥有的,但显然它的行为不正确,因为它试图调用函数Action\u 0
,而不是Action\u Overview
,给定示例url:
Route::set('default'), '<country>/<id>_<action>/<name>')
->defaults(array(
'controller' => 'index',
'action' => 'index'
));
Route::set('default'),'//'))
->默认值(数组)(
“控制器”=>“索引”,
'操作'=>'索引'
));
一个解决方案是将动作命名为动作0到动作9,但我想我们中的任何一个都会喜欢
另一个解决方案是在Controller::before()中“重新路由”操作,如下所示:
public function before()
{
parent::before();
$actions = array(0 => 'overview', ...);
$this->request->action($actions[$this->request->action()]);
}
public function action_route ()
{
$actions = array (0 => 'overview', ...);
$params = array (
'country' => $this->request->param ('country'),
'id' => $this->request->param ('id'),
'action' => $actions[$this->request->param ('oldaction')],
'name' => $this->request->param ('name'),
);
$this->request->redirect (Route::get ('legacy')->uri ($params));
}
Route::set('overview'), '<country>/<id>_0/<name>')
->defaults(array(
'controller' => 'index',
'action' => 'overview'
));
Route::set('details'), '<country>/<id>_1/<name>')
->defaults(array(
'controller' => 'index',
'action' => 'details'
));
或者,您可以在lambda/回调路由中执行此操作,但我会将其保留在Controller::before()中。使用干净的解决方案当然很好,但恐怕没有可用的解决方案。我可以想出一些方法来处理它,但是没有一种是理想的 第一名。将是Darsstar提供的子计算机思想 第2项。类似于第1项解决方案,但使用双路线系统,路线如下:
Route::set('default'), '<country>/<id>/<action>/<name>', array ('country' => '.+', 'name' => '.+', )) /* new */
->defaults(array(
'controller' => 'new',
'action' => 'index'
));
<强>>3,将简单地使用多条路线,如果动作的数量不是很大的话,考虑每个动作使用一条这样的路由:
public function before()
{
parent::before();
$actions = array(0 => 'overview', ...);
$this->request->action($actions[$this->request->action()]);
}
public function action_route ()
{
$actions = array (0 => 'overview', ...);
$params = array (
'country' => $this->request->param ('country'),
'id' => $this->request->param ('id'),
'action' => $actions[$this->request->param ('oldaction')],
'name' => $this->request->param ('name'),
);
$this->request->redirect (Route::get ('legacy')->uri ($params));
}
Route::set('overview'), '<country>/<id>_0/<name>')
->defaults(array(
'controller' => 'index',
'action' => 'overview'
));
Route::set('details'), '<country>/<id>_1/<name>')
->defaults(array(
'controller' => 'index',
'action' => 'details'
));
完成此操作后,您可以将控制器与操作一起使用action\u overview()
。在生产环境中,对路由使用缓存以避免在每次请求时重新定义它们
就个人而言,我会选择3号,如果它是一个直端口,并且没有计划扩展新功能的网站。否则我会选择no 2,因为它允许随着时间的推移逐步淘汰传统模式。此外,它还允许更灵活的“新”路线。我不会选择no 1,只是因为在我看来,它不符合Kohana风格的编码,我个人希望在bootstrap.php或init.php(对于模块)中找到路由规则。否则,这是一个完全有效的解决方案(从某种意义上说,这是Kohana的风格,因为Kohana具有足够的灵活性,可以提供与它的一般风格相反的解决方案……:)