[TOC]
# 路由
使用路由可以改寫Url地址,使其美觀和更容易被搜索引擎收錄
## **路由配置文件**
```
project 應用部署目錄
├─conf 配置文件目錄
│ └─route.php 路由配置信息
```
## **路由配置信息**
```
// +----------------------------------------------------------------------
// | 項目結構層數配置
// | open_level
// | ture:開啟項目結構
// | false:關閉項目結構目錄
// | 關閉后取SCRIPT_NAME 最后一個為Action 倒數第二個為Controller 參數/s區分
// +----------------------------------------------------------------------
//
'route' => [
'level' => 3, // 項目結構層數數量
'open_level' => false,
'open_route' => true, // 是否開啟路由轉換功能 true開啟 false關閉
'old_uri_hide' => false, // 當開啟路由轉換后是否禁止原路由訪問 false:不禁止 true:禁止
'route_files' => [CONFIG_PATH . 'route.php'], // 路由規則存放地址
'type' => 'sqlite', // 保存類型
],
```
>開啟路由需要修改配置信息 route.open_level=>true
>更改或者添加路由規則文件地址`修改/增加`route_files的值
## **創建路由**
`Route::rule('/hello/world',function(){echo 'hello world';});`
訪問`http://127.0.0.1/hello/world` 將直接打印出hello world信息
## **改寫控制器路由**
在route.php中填寫以下規則
`Route::rule('/home/hello/index','/');`
將`http://127.0.0.1/home/hello/index` 改寫成 `http://127.0.0.1`
## **禁止原路由訪問**
兩個地方都可以調整
>[info] 全局配置文件調整 route.old_uri_hide => true
>[info]單條路由調整 `Route::rule('/home/hello/index','/',['old_uri_hide'=>true]);`
>單條規則覆蓋全局規則
開啟該功能后 只能使用改寫路由訪問 不能使用原路由訪問
## **隱藏路由參數**
```
$params= ['test'=>'abc','type'=>1,'id'=>2];
Route::rule('/home/hello/detail', '/home/hello/detail', ['params'=>$params]);
```
訪問`http://127.0.0.1/home/hello/detail` 將直接獲取`GET['test']` `GET['type']` `GET['id']`
## **隱藏路由參數名稱**
```
Route::rule('detail', '/home/hello/detail', ['hiden_field'=>'id']);
```
訪問`http://127.0.0.1/detail/123` 將直接獲取`GET['id] => 123`
## **改寫路由后綴**
`Route::rule('/home/hello/detail', '/detail', ['suffix' => '.html']);`
將`http://127.0.0.1/home/hello/detail` 改寫成 `http://127.0.0.1/detail.html`