路由
作用
1.简化URL地址,方便大家记忆
2.有利于搜索引擎优化
入口文件
1.前后台分离
- 在网站public目录下新建admin.php
- 打开admin.php复制入口文件
2.绑定模块
-
实现功能 index.php只能去前台 admin.php只能去后台
define('BIND_MODULE','admin');
-
地址发生改变
未绑定:http://tp.com/index.php/admin/index/index 模块/控制器/方法
绑定后:http://tp.com/admin.php/index/index 控制器/方法
-
3.隐藏入口文件
- 找到文件apache/conf/http.conf 开启注释 *LoadModule rewrite_modules/mod_rewrite.so
- 在网站public目录下新建htaccess
4.重启服务,此时不再需要输入index.php这个入口文件
直接http://tp.com/index/index 就可以访问
规则
1.支持三种URL解析规则
2.只针对应用不针对模块,因此路由设置也是针对应用下的所有模块
3.关闭后台模块的路由可参考手册
路由模式
普通模式
关闭路由,完全使用PATH_INFO下的URL地址 如http://tp.com/index.php/index/index
跟配置文件有关
//是否开启路由
'url_route_on' =>false,
//是否强制启用路由
'url_route_must'=>false,
混合模式
//是否开启路由
'url_route_on' =>true,
//是否强制启用路由
'url_route_must'=>false,
强制模式
//是否开启路由
'url_route_on' =>true,
//是否强制启用路由
'url_route_must'=>true,
路由定义
Route::rule(‘路由表达式’,‘路由地址’,‘请求类型’,‘路由参数(数组)’,‘变量规则(数组)’);
表达式: 网址栏输入的名字
路由地址:代替的访问地址
请求类型:get,post,put,delete
路由参数:ext…
变量规则:各种正则表达式
tp5/application/route.php
<?php
use think\Route;
//注册路由器到index模块的user控制器的read方法
Route::rule('new/:id','index/User/read');
访问http://tp.com/index.php/new/5 就会调用方法
动态单个设置
0.设置路由请求方式
-
请求类型 get post put delete
-
Route::rule()默认支持所有请求类型
Route::get('test','index/index/test'); Route::rule('test','index/index/test','get|post');
-
模拟put请求(请求伪装)
<form method="post" action="type"> <input type="hidden" name="_method" value="PUT"> <input type="text" name="name" id=""> <input type="submit" value="提交"> </form>
1.设置路由文件
(首先要改配置文件)
tp5/application/route.php
2.如何设置
//引入系统类
use think\Route;
//定义路由规则
//访问斜杠直接访问到index/index/index
Route::rule('/',index/index/index);
Route::rule('test','index/index/test');
3.路由的形式
-
静态地址路由
-
带参数路由
使用input方法(路由设置几个参数就必须带几个参数)
访问地址 http:www.tp.com/course/1
设置route.php文件
Route::rule('course/:id','index/index/course');
-
可选参数
设置route.php文件
Route::rule('course/[:id]','index/index/course');
-
全动态路由(不建议使用)
Route::rule(':a/:b','index/index/course');
-
完全匹配路由
Route::rule('test$','index/index/course');
-
路由额外带参数
Route::rule('blog/:id','blog/read?status=1&app_id=5');
4.发送请求
-
tp5/application/index/view/index/type.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>$Title$</title> </head> <body> <form method="post" action="type"> <!-- <input type="hidden" name="_method" value="PUT">--> <label for=""></label><input type="text" name="name" id=""> <input type="submit" value="提交"> </form> </body> </html>
-
tp5/application/index/controller/index.php
<?php namespace app\index\controller; class Index { public function index() { dump(input()); } public function type() { echo "type"; echo dump(input()); //引入页面type.php return view(); } }
-
访问网址http://tp.com/index.php/index/type