您的位置:首页 > 编程语言 > PHP开发

thinkphp5.0学习(四):入口文件、路由模式、路由设置和url生成

2017-10-23 11:14 656 查看

一、路由的作用

简化URL地址,方便记忆

有利于搜索引擎的优化

二、入口文件

前后台分离

在网站public目录下(项目\public)新建admin.php

打开admin.php

<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------

// [ 应用入口文件 ]

// 定义应用目录
define('APP_PATH', __DIR__ . '/../application/');
// 加载框架引导文件
require __DIR__ . '/../thinkphp/start.php';


绑定模块

实现功能

index.php 这个入口文件,只能去前台模块

admin.php这个入口文件,只能去后台模块(建议后台入口文件复杂一些)

如何实现

在入口文件中

// 定义前台
define('BIND_MODULE', 'index');
// 绑定后台
define('BIND_MODULE', 'admin');


URL地址发生变化

入口绑定之前(http://www.tp.com/admin.php/模块/控制器/方法)

入口绑定之后(http://www.tp.com/admin.php/控制器/方法)

隐藏入口文件

开启apache重写(D:\wamp64\bin\apache\apache2.4.23\conf\httpd.conf)

把注释开启 LoadModule rewrite_module modules/mod_rewrite.so

设置访问权限(D:\wamp64\bin\apache\apache2.4.23\conf\extra\httpd-vhosts.conf)

<VirtualHost *:80>
ServerName www.tp.com
DocumentRoot D:/wamp64/www/study/thinkphpstudy/public
<Directory  "D:/wamp64/www/study/thinkphpstudy/public">
Options +Indexes +Includes +FollowSymLinks +MultiViews
AllowOverride All
Require all granted
</Directory>
</VirtualHost>


入口文件,在网站public目录下新建.htaccess文件,

<IfModule mod_rewrite.c>
Options +FollowSymlinks -Multiviews
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L]
</IfModule>


重启服务

url地址变化

隐藏之前http://www.tp.com/index.php/控制器/方法

隐藏之后http://www.tp.com/控制器/方法

三、tp5.0路由学习注意

支持三种方式的url解析规则

路由只针对应用,不针对模块,因此路由的设置也是针对应用下的所有模块。

关闭后台模块,在后台入口文件中(admin.php),写在加载框架引导文件之后,否则报错。

<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------

// [ 应用入口文件 ]

// 定义应用目录
define('APP_PATH', __DIR__ . '/../application/');
// 绑定后台
define('BIND_MODULE', 'admin');
// 加载框架引导文件
require __DIR__ . '/../thinkphp/start.php';

// 关闭admin模块的路由
\think\App::route(false);


路由的三种模式

普通模式

1.定义

关闭路由,完全使用默认的PATH_INFO方式URL:

2.形式

http://www.tp.com/admin.php/index/index

3.如何设置

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


混合模式

1.定义

开启路由,并使用路由定义+默认PATH_INFO方式的混合

2.如何设置

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


强制模式

1.定义

开启路由,并设置必需定义路由才能访问

2.如何设置

// 是否开启路由
'url_route_on'           => true,
// 是否强制使用路由
'url_route_must'         => true,


四、设置路由

1.动态单个注册

设置路由格式

Route::rule(‘路由表达式’, ‘路由地址’, ‘请求类型’, ‘路由参数(数组)’, ‘变量规则(数组)’)

设置路由文件(项目\application\route.php)

如何设置(route.php)

use think\Route;
// 定义路由规则
// 设置路由之后,就不能使用pathinfo访问了
Route::rule('/','index/index/index');
//注册路由访问到index模块下的index控制器下的index的方法
Route::rule('test','index/index/test');
//注册路由test 访问到index模块下的index控制器下的test的方法


路由的形式

1、静态地址路由

//注册路由test 访问到index模块下的index控制器下的test的方法
Route::rule('test','index/index/test');


2、给路由带参数

route.php中
//注册带参数路由
//http://www.tp.com/course/1
//http://www.tp.com/index/index/index/id/1
Route::rule('course/:id','index/index/course');


index.php中
function course(){
return input('id');
}


3、给路由带多个参数(设置了带几个就必需带几个)

route.php中
//注册带参数路由
//http://www.tp.com/time/1/2
//http://www.tp.com/index/index/shijian/year/1/month/2
Route::rule('time/:year/:month','index/index/shijian');


index.php中
function shijian(){
return input('year').input('month');
}


4、可选参数路由

route.php中
//注册带可选参数路由
//http://www.tp.com/time/1
//http://www.tp.com/index/index/shijian/year/1
Route::rule('time/:year/[:month]','index/index/shijian');


index.php中
function shijian(){
return input('year').input('month');
}


5、全动态路由(不建议使用)

route.php中
//注册带可选参数路由
//http://www.tp.com/1/1
//http://www.tp.com/index/index/dongtai/1/1
Route::rule(':a/:b','index/index/dongtai');


index.php中
function dongtai(){
return input('a').input('b');
}


6、完全匹配路由

route.php中
//注册带可选参数路由
//http://www.tp.com/1/1
//http://www.tp.com/index/index/dongtai/1/1
Route::rule(':a/:b$','index/index/dongtai');


index.php中
function dongtai(){
return input('a').input('b');
}


7、带额外参数

route.php中
// 带额外参数
Route::rule('test2','index/index/test2?id=10&name=tian');


index.php中
function test2(){
dump(input());
}


设置请求类型

1.TP中请求类型

get,post,put,delete

2.Route::rule() 默认支持所有类型

3.设置各种请求

// 支持get请求的两种方式
Route::rule('type','index/index/type','get');
Route::get('type','index/index/type');
// 支持post请求的两种方式
Route::rule('type','index/index/type','post');
Route::post('type','index/index/type');
// 同时支持get和post
Route::rule('type','index/index/type','get|post');
// 支持所有路由
Route::rule('type','index/index/type','*');
Route::any('type','index/index/type' );
// 支持put请求
Route::rule('type','index/index/type','put');
Route::put('type','index/index/type' );
// 支持delete请求
Route::rule('type','index/index/type','delete');
Route::delete('type','index/index/type' );


4.如何模拟put和delete请求

<form action="type" method="post">
<p>
<input type="hidden" name="_method" value="PUT" />
<input type="text" name="name" id="" />
</p >
<p>
<input type="submit" value="提交" />
</p >
</form>


2.设置路由-动态批量注册

1.基本格式

Route::rule([
'路由规则1'=>'路由地址和参数',
'路由规则2'=>['路由地址和参数','匹配参数(数组)','变量规则(数组)'],
...
],'','请求类型','匹配参数(数组)','变量规则');


2.使用

// 动态批量注册
Route::rule([
"index"=>"index/index/index",
"diaoyong"=>"index/index/diaoyong",
"type/:id"=>"index/index/type"
],'','get');
Route::get([
"index"=>"index/index/index",
"diaoyong"=>"index/index/diaoyong",
"type/:id"=>"index/index/type"
]);


3.设置路由-配置文件批量注册

return [
"index"=>"index/index/index",
"diaoyong"=>"index/index/diaoyong",
"type/:id"=>"index/index/type"
];


五、变量规则

Route::rule(‘路由表达式’,’路由地址’,’请求类型’,’路由参数(数组)’,’变量规则(数组)’);

// ['id'=>'\d{1,3}','name'=>'\w+']设置路由变量规则,id只能是1-3位数字,name只能是hi字符串
Route::rule("course/:id/:name","index/index/course",'get',[],['id'=>'\d{1,3}','name'=>'\w+']);


六、路由参数

路由参数是指可以设置一些路由匹配的条件参数,主要用于验证当前的路由规则是否有效,主要包括:

Route::rule('course/:id/:name','index/index/course','get',['method'=>'get','ext'=>'html'],['id'=>'\d{1,3}','name'=>'\w+']);
// 路由参数method 请求方式必需是get
// 路由参数ext 主要设置路由的后缀

参数  说明
method  请求类型检测,支持多个请求类型
ext URL后缀检测,支持匹配多个后缀
deny_ext    URL禁止后缀检测,支持匹配多个后缀
https   检测是否https请求
domain  域名检测
before_behavior 前置行为(检测)
after_behavior  后置行为(执行)
callback    自定义检测方法
merge_extra_vars    合并额外参数
bind_model  绑定模型(V5.0.1+)
cache   请求缓存(V5.0.1+)
param_depr  路由参数分隔符(V5.0.2+)
ajax    Ajax检测(V5.0.2+)
pjax    Pjax检测(V5.0.2+)


七、资源路由

1.声明

Route::resource(‘blog’,’index/blog’);

也可以在定义资源路由的时候限定执行的方法(标识),例如:

Route::resource('blog','index/blog',['only'=>['index','read','edit','update']]);
Route::resource('blog','index/blog',['except'=>['index','delete']]);


2.会动注册7个路由规则(一定要记忆)

标识请求类型生成路由规则对应操作方法(默认)
indexGETblogindex
createGETblog/createcreate
savePOSTblogsave
readGETblog/:idread
editGETblog/:id/editedit
updatePUTblog/:idupdate
deleteDELETEblog/:iddelete

八、快捷路由

1.声明

// 声明快捷路由
Route::controller('blog','index/blog');


2.控制器

class Blog
{
public function geta(){
echo 'aaaaaaaaa';
}
}


3.url访问

https://www.tp.com/blog/a (寻找geta方法)

https://www.tp.com/blog/index (寻找getindex方法)

九、url生成

1.系统类

dump(Url::build('index/index/index'));


2.系统方法

dump(url('index/index/index'));


3.使用

public function index()
{
echo '我是blog控制器index方法';
dump(Url::build('index/index/index'));dump(url('index/index/index'));
dump(Url::build('index/index/test'));
dump(url('index/index/test'));

dump(Url::build('index/index/course/id/10'));
dump(url('index/index/course/id/10'));
//
dump(Url::build('index/index/abc',['id'=>10,'name'=>'张三']));
dump(url('index/index/abc',['id'=>10,'name'=>'张三']));
dump(url('index/index/abc','id=10&name=100'));

//带锚点
dump(url('index/blog/read#name','id=5'));
dump(url('index/blog/read#name',['id'=>5,'name'=>'100']));
// 带域名
dump(Url::build('index/blog/read#anchor@blog','id=5'));
dump(url('index/blog/read#anchor@blog',['id'=>5,'name'=>'100'])); http://blog.tp.com/blog/read/id/5/name/100.html#anchor
// 加上入口文件
Url::root('/index.php');
dump(url('index/blog/read#anchor@blog',['id'=>5,'name'=>'100']));
//http://blog.tp.com/index.php/blog/read/id/5/name/100.html#anchor

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  thinkphp