Kohana- 路由基础

下面你可以看到在application/bootstrap.php文件预定义的默认Route。

每个路由必须一个uri的名字和一套默认值

<pre lang="php>;  

Route::set('default', '((/(/)))')       ->defaults(array(           'controller' => 'welcome',           'action' => 'index',       ));

这条路由将处理下面格式化的方式。

http://example.php/index.php/<controller>/<action>/<id> </id></action></controller>

所有的URI请求字段都是可选的,倘若不支持路由将使用默认值
下面是同样的路由,但是必须提供controller和action字段取代了可选

  Route::set('required', '<controller>/<action>(/<id>)')   
    ->defaults(array(
    'controller' => 'welcome',          
     'action' => 'index',      
     )); 
</id></action></controller>

一个基本的自定义路由
重要:想创建自定义路由必须定义在default路由前。默认路由应该保持在最后定义。
下面的ex路由将所有请求ample.com/index.php/products//指向合适的inventory控制器

  Route::set('products', 'products(/<action>(/<id>)))')       
->defaults(array(        
   'controller' => 'inventory',    
       'action' => 'index',   
    ));  
</id></action>

一个没有可选项字段的自定义路由
下面的所有的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',
  ));
 </page>

一个包含id和slug的的自定义路由
必须设置在default路由前

    Route::set('idslug', '<controller>/<action>/<id>-<slug>', 
array('id'=>'[0-9]+', 'slug'=>'.+'))
 ->defaults(array( 
'controller' => 'home', 
'action'     => 'index',
 ));
 </slug></id></action></controller>


忽略路由溢出

默认路由像这样

controller/action/id

你可能需要你的路由支持以下的格式

controller/action/id/and/anything/else/you/want/to/add/at/random

你需要修改默认路由,并在application/bootstrap.php添加一条正则表达式

  Route::set('default', '(<controller>(/<action>(/<id>(/<stuff>))))', array('stuff' => '.*?'))
      ->defaults(array(
          'controller' => 'welcome',
          'action' => 'index',
  ));</stuff></id></action></controller>


你可能感兴趣的:(Kohana- 路由基础)