路由基础–kohana3使用手册
Kohana 3.0 中的 路由类支持你无限制的配置特殊格式的请求来映射到指定的控制器和动作上。
你可以在 application/bootstrap.php 文件或者在 modules/我的模块/init.php 文件中调用 Route::set() 方法来定义路由
你可以在 application/bootstrap.php 文件中看到如下的默认路由。每个路由至少有一个名称一个 uri 以及给 uri 设置一个默认值。
Route::set(‘default’, ‘(<controller>(/<action>(/<id>)))’)
->defaults(array(
‘controller’ => ‘welcome’,
‘action’ => ‘index’,
));
这个路由将会处理诸如下列格式的请求。
http://example.php/index.php/<controller>/<action>/<id>
这个请求的 URI 中所有部分都是可选的。如果路由中的某一部分没有指明,那么路由会使用默认值。
这里有一个相同的路由,但是控制器和动作部分都不能可选。
Route::set(‘required’, ‘<controller>/<action>(/<id>)’)
->defaults(array(
‘controller’ => ‘welcome’,
‘action’ => ‘index’,
));
一个基本的定制路由
重点:当创建定制路由时,它们总是要定义在默认路由之前。你的默认路由总是定义在最后。
下列路由将把所有 example.com/index.php/products/<action>/<id> 的请求转到 inventory 控制器的动作上。
Route::set(‘products’, ‘products(/<action>(/<id>))’)
->defaults(array(
‘controller’ => ‘inventory’,
‘action’ => ‘index’,
));
一个没有可选部分的定制路由
下面的路由将 example.com/index.php/custom 请求转到了 welcome控制器的 index 动作上。
Route::set(‘custom’, ‘custom’)
->defaults(array(
‘controller’ => ‘welcome’,
‘action’ => ‘index’,
));
只有一个动态部分的定制路由
下面的路由将 example.com/index.php/about, example.com/index.php/faq 和 example.com/index.php/locations 请求转到了 page 控制器的 static 动作上
Route::set(‘static’, ‘<page>’, array(‘page’ => ‘about|faq|locations’))
->defaults(array(
‘controller’ => ‘page’,
‘action’ => ‘static’,
));
含有 id 和 slug 的定制路由
必须设置在 defaut 路由之前
Route::set(‘idslug’, ‘<controller>/<action>/<id>-<slug>’, array(‘id’=>’[0-9]+’, ‘slug’=>’.+’))
->defaults(array(
‘controller’ => ‘home’,
‘action’ => ‘index’,
));