Tp5入门——路由入门与实战

路由

官方文档里面是这样子定义的=>路由的作用是简化URL访问地址,并根据定义的路由类型做出正确的解析。

一串url输进去之后,就会感觉很绝望,(小声吐槽)=>“我只是想测试一个小接口让我打这么长一串地址、地址太长记不住呀……”。这个时候我们就可以用路由去简化他的地址,并且在缩短地址的同时进行一些规则的定义。

  • 路由的作用:
    1. 简化URL地址,方便大家记忆
    2. 有利于搜索引擎的优化,比如可以被百度的爬虫抓取到

注意:路由只针对应用,不针对模块,如果有些模块不像使用路由,需要在配置文件中关闭路由

1
2
//关闭admin模块的路由,必须写到加载框架引导文件之后
\think\App::route(false);

1.普通模式:完全使用PASH_INFO来访问

1
2
3
4
// 是否开启路由
'url_route_on' => false,
// 是否强制使用路由
'url_route_must' => false,

2.混合模式

1
2
3
4
// 是否开启路由
'url_route_on' => true,
// 是否强制使用路由
'url_route_must' => false,

3.强制模式

1
2
3
4
// 是否开启路由
'url_route_on' => true,
// 是否强制使用路由
'url_route_must' => false,

设置路由:

一下直接修改application下的route.php

1.单个注册

在application下的route.php文件内更改

1
2
use think\Route;                        //引入Route           
Route::rule('test','index/index/test'); //当URL访问http://com.test.php:8888/test时,访问的是index模块下的index下的控制器下的test方法
路由形式:
  1. 静态路由:
    Route::rule(‘/’,’index/index/index’);
    Route::rule(‘test’,’index/index/test’);
  2. 带参数的路由: Route::rule(‘time/:time’,’index/User/getTime’);
    http://com.test.php:8888/getTime/123456
  3. 可选参数的路由: Route::rule(‘time/[:time]/[:???]’,’index/User/getTime’);
    http://com.test.php:8888/getTime
    http://com.test.php:8888/getTime/123456
  4. 全动态路由(不推荐): Route::rule(‘:X/:Y’,’index/User/getXY’);
    http://com.test.php:8888/11/22
  5. 完全匹配:Route::rule(‘rout$’=>’index/User/getRout’);
    不加$符号,我们字comp后面加多少路径,比如http://com.test.php:8888/comp/asd/dfds/wer/ewr,页面都能输出 我是完全匹配路由 Route::rule(‘rout’,’index/User/getRout’);
    加上$符号,我们在comp后面加多少路径,比如http://com.test.php:8888/comp/comp/asd/dfds/wer/ewr,页面不能输出方法的内容
  6. 额外参数:Route::rule(‘test’,’index/index/test?id=1&name=icocos’);

2.批量注册

  • rule方式
1
Route::rule([ "test"=>"index/index/test", 'time/:time'=>'index/User/getTime' ],'','get'); //post,put,delete
  • get方式
1
Route::get([ "test"=>"index/index/test", 'time/:time'=>'index/User/getTime' ]); //post,put,delete

3.配置文件注册

1
return[ "test"=>"index/index/test", 'time/:time'=>'index/User/getTime'];

路由的请求方式

一般请求方式有八种,但是TP里面有四种请求方式(GET,POST,PUT,DELETE),其中GET,POST是最常用的,如果我们不指定请求类型,默认是*,所有的请求类型

  • 支持get
1
2
Route::rule('rtype','index/User/getRType','get');
Route::get('rtype','index/User/getRType');
  • 支持post
1
Route::post('rtype','index/User/getRType');
  • 既支持get有支持post
1
Route::rule('rtype','index/User/getRType','get|post');
  • 支持全部请求方式
1
2
Route::any('rtype','index/User/getRType');
Route::rule('rtype','index/User/getRType','*');

模拟PUT,DELETE

  • 支持put请求
1
2
Route::put('rtype','index/User/getRType');
Route::rule('rtype','index/User/getRType','put');
  • 支持put请求
1
2
Route::delete('rtype','index/User/getRType');
Route::rule('rtype','index/User/getRType','delete');

变量规则

  • Route::rule();的最后一个参数,是一个数组,可以指定多个参数,用正则表达式来写,用来规范传入的参数必须是什么数据类型,或者必须是那些数据等等,比如
1
Route::rule('getid/:id','index/User/getId','get',[],['id'=>'\d']);  //最后一个参数,表示id传参数必须是数字

路由参数

  • Route::rule();的倒数第二个参数,是一个数组,可以用来指定请求的数据类型,也可以用来规定请求的URL后缀,比如
1
2
Route::rule('getid/:id','index/User/getId','get',['method'=>'get','ext'=>'html'],['id'=>'\d']);
//请求方式必须是get,请求的后缀必须是html,访问的url为http://www.yd.com/getid/9.html,不带html后缀就请求失败

资源路由

你的后台模块可能会有增删改查等操作,但是一个一个写太费劲,资源路由自动帮你生这些路由,你只需要在控制器内写这些方法,

设置后会自动注册7个路由规则,如下:

1
2
3
4
5
6
7
8
标识    请求类型    生成路由规则    对应操作方法(默认)
index GET blog index
create GET blog/create create
save POST blog save
read GET blog/:id read
edit GET blog/:id/edit edit
update PUT blog/:id update
delete DELETE blog/:id delete
实战
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//先创建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');
效果:
1
2
3
//当你访问http://com.test.php:8888/block         URL访问的是index方法
//当你访问http://com.test.php:8888/block/15 URL访问的是read方法
//当你访问http://com.test.php:8888/block/create URL访问的是create方法

快捷路由

在index模块下创建一个Fastroute控制器,里面写下如下例子,除了index,其他方法都要加上get

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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里面写下快捷路由

1
2
3
4
//注意:路由名字要和控制器名字一样
Route::controller('Fastroute','index/Fastroute');
//然后我们想访问getAA方法,我们可以通过访问URL http://com.test.php:8888/Fastroute/AA来访问
//想访问getBB(),可以通过 http://com.test.php:8888/Fastroute/BB来访问

生成URL:

  • 有两种方式
1
2
Url::build(‘index/User/index’); 
url('index/User/index');

查看生成方式

1
2
dump(url('index/User/index'));
dump(Url::build('index/User/index'));
  • 带参数
1
2
3
url('index/User/index/id/10');
url('index/User/index/abc',['id'=>10,'name'=>'icocos']);
url('index/User/index/abc', 'id=10&name=icocos');
  • 带锚点
1
url('index/User/index/abc#name', 'id=10&name=icocos');
  • 带域名
1
url('index/User/index/abc#name@blog', 'id=10&name=icocos');

加入口文件

1
Url::root('/index.php');            //带入口文件
坚持原创技术分享,您的支持将鼓励我继续创作!