🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
[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`