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>


转载于:https://my.oschina.net/u/1757002/blog/308078

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值