多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
# 路由定义方法 ``` Route::rule(路由表达式,路由地址,请求类型); ``` # 基本使用方式 ## 第一步:在index模块建立News控制器 ```php <?php namespace app\index\controller; use think\Controller; class News extends Controller { public function read($id){ echo '我今年读了'.$id.'本书!'; } } ``` ## 第二步:在route/route.php文件里写入 ~~~ <?php Route::rule('read/:id','index/news/read'); ~~~ ## 第三步:在浏览器上显示 ![](https://box.kancloud.cn/9adb70f04d3ab74fb2001d0b4b1a9041_379x109.jpg) ***** # 第二个例子: 静态地址和动态地址结合 ## 第一步:在index模块建立News控制器 ~~~ <?php namespace app\index\controller; use think\Controller; class News extends Controller { public function read($year,$month,$day){ echo $year.'年'.$month.'月'.$day.'日,我正在读书'; } } ~~~ ## 第二步:在route/route.php文件里写入 ~~~ Route::rule('read/:year/:month/:day','index/news/read'); ~~~ ## 第三步:在浏览器上显示 ![](https://box.kancloud.cn/de808fd2aeb0b845c5b7bffad1974cb3_424x105.jpg) ***** # 第三个例子:全动态地址 ## 第一步:在index模块建立Blog控制器 ~~~ <?php namespace app\index\controller; use think\Controller; class Blog extends Controller { public function read($user,$blog_id){ echo $user.'的'.'博客id是'.$blog_id; } } ~~~ ## 第二步:在route/route.php文件里写入 ~~~ <?php Route::get(':user/:blog_id','Blog/read'); ~~~ ## 第三步:在浏览器上显示 ![](https://box.kancloud.cn/4cb4db8d8ce07d3c8c2710ef183e2b0d_397x107.jpg) ***** # 第四个例子:可选定义 ## 第一步:在index模块建立Blog控制器 ~~~ public function archive($year,$month=null){ echo '我在'.$year.'年'.$month.'月'.'发不了一个博客'; } ~~~ ## 第二步:在route/route.php文件里写入 ~~~ <?php Route::get('blog/:year/[:month]','Blog/archive'); ~~~ ## 第三步:在浏览器上显示 ![](https://box.kancloud.cn/c9ad50463a80d63a554b243859481b93_390x100.jpg)