guss77 / kohana-routing
Kohana >= 3.3 模块,通过配置文件设置路由
1.1.4
2016-03-31 06:28 UTC
Requires
- php: >=5.4
- composer/installers: ~1.0
- kohana/core: >=3.3
README
停止使用 bootstrap.php 来设置您的 Kohana 路由 - 现在路由只是一个配置文件!
安装
- 将
guss77/kohana-routing
添加到您的composer.json
,或者运行composer require guss77/kohana-routing
- 编辑您的
bootstrap.php
以添加kohana-routing
模块,并从文件底部移除Route::set()
调用
内置的配置文件包含默认 Route::set()
代码的替代品。
用法
不要在 bootstrap.php
代码中添加 Route::set()
调用,而是将模块中的 routes.php
配置文件复制到您的应用程序中,并在那里添加规则。
配置格式非常简单 - 它是一个路由规则的数组,其中数组的每个顶级元素代表一个单独的 Route::set
调用。对于每个规则
- 规则的“键”是路由的名称,对应于
Route::set()
的第一个参数 - 规则的“值”是一个包含以下键的数组
- 'uri' - 解析 URI 的规则表达式,对应于
Route::set()
的第二个参数 - 'defaults' - 可选数组,定义了路由表达式值的默认值,对应于
Route
实例上的defaults()
调用 - 'rules' - 可选数组,包含正则表达式,用于限制路由表达式的匹配,仅匹配具有有效值的参数。这对应于
Route::set()
的第三个参数
- 'uri' - 解析 URI 的规则表达式,对应于
示例配置
以下代码片段演示了如何在配置文件中设置规则,但省略了配置文件的样板(PHP 声明和返回数组语法)。
- 默认 Kohana 路由
'default' => [
'uri' => '(<controller>(/<action>(/<id>)))',
'defaults' => [
'controller' => 'welcome',
'action' => 'index',
],
],
- 从子目录加载控制器的路由
'admins' => [
'uri' => 'backoffice/(controller(/<action>))',
'defaults' => [
'directory' => 'admin',
'controller' => 'backoffice',
'action' => 'menu',
],
],
'clients' => [
'uri' => '(/controller(/<action>))',
'defaults' => [
'directory' => 'users',
'controller' => 'welcome',
'action' => 'index',
],
],
- 具有参数验证的路由
'categories' => [
'uri' => 'cat/<category>',
'rules' => [
'category' => '[a-z\-_\.]+',
],
'defaults' => [
'controller' => 'categories',
'action' => 'category',
],
],