admin管理员组文章数量:1633792
路由的作用:
1. 简化URL地址,方便大家记忆
2. 有利于搜索引擎的优化,比如可以被百度的爬虫抓取到
优化URl
1. 前后端分离
修改入口文件,在public下新建admin.php文件,将下面的代码添加进入
// 定义应用目录
define('APP_PATH', __DIR__ . '/../application/');
// 加载框架引导文件
require __DIR__ . '/../thinkphp/start.php';
2.绑定模块
1)前后端分离实现的功能
index.php 这个入口文件只能进入前台模块
admin.php 这个入口文件只能进入后台模块
2)绑定模块
在index.php添加 define(‘BIND_MODULE’,’index’); 这样http://www.demo/index.php/只能访问前台模块。访问不了后台,http://www.yd/index.php/index/index
在admin.php添加 define(‘BIND_MODULE’,’admin’); 这样http://www.demo/admin.php只能访问后台模块,访问不了前台,http://www.yd/admin.php/index/index
3) 隐藏入口文件(怎么操作就不写了,可以看下文档里面的URL访问下 的隐藏入口文件 的说明),这样访问前台模块可以省去index.php,可以用http://www.yd/index/index直接访问到
关闭后台的路由
在public下的admin.php中添加这句代码 \think\App::route(false);
// 定义应用目录
define('APP_PATH', __DIR__ . '/../application/');
//绑定后台
define('BIND_MODULE','admin');
// 加载框架引导文件
require __DIR__ . '/../thinkphp/start.php';
//关闭admin模块的路由,必须写到加载框架引导文件之后
\think\App::route(false);
路由的三种模式:
1. 普通模式 :完全使用PASH_INFO来访问,比如http://www.yd/index.php/index/index,域名+模块+控制器
2. 混合模式 :可以使用路由也可以不使用
3. 强制模式 :必须使用路由
设置路由
一.动态单个设置
在application下的route.php文件内更改
use think\Route; //引入Route
Route::rule('test','index/index/demo'); //当URL访问http://www.yd/test时,访问的是index模块下的index下的控制器下的demo方法
路由形式:
静态路由:Route::rule(‘test’,’index/index/demo’);
带参数的路由: Route::rule(‘getid/:id’,’index/User/getId’);
比如我访问http://www.yd/getid/7,或者http://www.yd/getid/8,或者http://www.yd/getid/9,就是getid后面带个参数
//首先在index模块下的User控制器中写一个getId方法
public function getId(){
echo input('id'); //输出id
}
//然后在route.php加上这行代码
Route::rule('getid/:id','index/User/getId');
//最后当我们http://www.yd/getid后面加个数字,比如http://www.yd/getid/20,页面会显示20
带多个参数路由,比如带两个参数
//index模块下的User控制器中写一个myTime方法
public function myTime(){
echo input('year').' 年 '.input('month').'月'; //输出 n年n月
}
//然后在route.php加上这行代码
Route::rule('time/:year/:month','index/User/myTime');
//最后当我们访问http://www.yd/time/2018/9,页面会显示2018 年 9月
选择性带参数,就是我们在访问url时,URL后面可以带参数,也可以不带,在写路由文件上的参数带上中括号就行
比如输出年或年月
public function myTime(){
echo input('year').' 年 '.input('month').'月'; //输出 n年n月
}
//然后在route.php加上这行代码
Route::rule('time/:year/[:month]','index/User/myTime'); //重点:month外面加[]
//最后当我们访问http://www.yd/time/2018/9,页面会显示2018 年 9月
//当我们访问http://www.yd/time/2018,页面会显示2018 年 月
纯带参数的路由 不建议使用
//路由写法
Route::rule(':x/:y','index/User/XAndY');
//方法
public function XAndY(){
echo input('x').' '.input('y');
}
//访问http://www.yd/5/3,页面输出5 3
完全匹配路由 在路由的后面价格$符号
public function comp(){
echo '我是完全匹配路由';
}
//不加$符号,我们字comp后面加多少路径,比如http://www.yd/comp/asdfda/asdfasfd/aaa/bbb,页面都能输出 我是完全匹配路由
Route::rule('comp','index/User/comp');
//加上$符号,我们在comp后面加多少路径,比如http://www.yd/comp/asdfda/asdfasfd/aaa/bbb,页面不能输出方法的内容
Route::rule('comp','index/User/comp');
二.批量设置路由
第一种写法,将上面所有单个动态注册的路由批量注册
Route::rule([
"test"=>"index/index/demo",
'getid/:id'=>'index/User/getId',
'time/:year/[:month]'=>'index/User/myTime',
':x/:y'=>'index/User/XAndY',
'comp$'=>'index/User/comp'
],'','get');
第二种方式,这里用get举例
Route::get([
"test"=>"index/index/demo",
'getid/:id'=>'index/User/getId',
'time/:year/[:month]'=>'index/User/myTime',
':x/:y'=>'index/User/XAndY',
'comp$'=>'index/User/comp'
]);
3.配置文件设置路由,使用配置文件批量注册,还是在route.php文件内写
return[
"test"=>"index/index/demo",
'getid/:id'=>'index/User/getId',
'time/:year/[:month]'=>'index/User/myTime',
':x/:y'=>'index/User/XAndY',
'comp$'=>'index/User/comp'
];
路由的请求方式
TP里面有四种请求方式,GET,POST,PUT,DELETE四种方式,如果我们不指定请求类型,默认是*,所有的请求类型
请求方式有两种写法,这里用get举例
Route::rule('qtype','index/User/questType','get');
Route::get('gtype','index/User/questType');
既支持get有支持post的写法
Route::rule('type','index/User/questType','get|post');
全部请求方式都支持的两种写法
Route::any('type','index/User/questType');
Route::rule('type','index/User/questType','*');
变量规则,Route::rule();的最后一个参数,是一个数组,可以指定多个参数,用正则表达式来写,用来规范传入的参数必须是什么数据类型,或者必须是那些数据等等,比如
Route::rule('getid/:id','index/User/getId','get',[],['id'=>'\d']); //最后一个参数,表示id传参数必须是数字
路由参数,Route::rule();的倒数第二个参数,是一个数组,可以用来指定请求的数据类型,也可以用来规定请求的URL后缀,比如
Route::rule('getid/:id','index/User/getId','get',['method'=>'get','ext'=>'html'],['id'=>'\d']);
//请求方式必须是get,请求的后缀必须是html,访问的url为http://www.yd/getid/9.html,不带html后缀就请求失败
资源路由,你的后台模块可能会有增删改查等操作,但是一个一个写太费劲,资源路由自动帮你生这些路由,你只需要在控制器内写这些方法,
比如
//先创建block
namespace app\index\controller;
class Block
{
public function index(){
echo '我是前台模块下的block';
}
public function create(){
echo '我是前台模块下的block的create方法';
}
public function read($id){
echo $id;
}
}
//然后在route.php下写上资源路由
Route::resource('block','index/Block');
//效果:
//当你访问http://www.yd/block URL访问的是index方法
//当你访问http://www.yd/block/15 URL访问的是read方法
//当你访问http://www.yd/block/create URL访问的是create方法
快捷路由
在index模块下创建一个Fastroute控制器,里面写下如下例子,除了index,其他方法都要加上get
namespace app\index\controller;
class Fastroute
{
public function index(){
echo '我是Fast路由的index';
}
public function getAA(){
echo "我是getAA";
}
public function getBB(){
echo "我是BB";
}
public function postInfo()
{
}
public function putInfo()
{
}
public function deleteInfo()
{
}
}
在route.php里面写下快捷路由
//注意:路由名字要和控制器名字一样
Route::controller('Fastroute','index/Fastroute');
//然后我们想访问getAA方法,我们可以通过访问URL http://www.yd/Fastroute/AA来访问
//想访问getBB(),可以通过 http://www.yd/Fastroute/BB来访问
生成URL 两种方式,不太懂有什么用
Url::build(‘index/User/index’);
Url::build();
Url::root('/index.php'); //带入口文件
dump(Url('index/User/index'));
dump(Url::build('index/User/index'));
本文标签: 路由
版权声明:本文标题:ThinkPHP5之路由 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://www.elefans.com/dianzi/1729178383a1189006.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论