a、在网站public目录下新建admin.phpphp
b、打开admin.phphtml
<?php
// [ 应用入口文件 ]
// 定义应用目录
define('APP_PATH', __DIR__ . '/../application/');
// 加载框架引导文件
require __DIR__ . '/../thinkphp/start.php';
一、实现功能thinkphp
index.php 这个入口文件 只能去前台模块apache
admin.php 这个入口文件 只能去后台模块 建议后台的入口文件稍微复杂些后端
二、如何实现数组
在入口文件中 app
define('BIND_MODULE','index');//绑定前台模块
define('BIND_MODULE','admin');//绑定后台模块
三、url地址框架
a、入口绑定模块以前前后端分离
http://www.tp5.com/入口文件/模块/控制器/操做iview
b、入口绑定模块以后
http://www.tp5.com/入口文件/控制器/操做
一、开始apache 的重写 F:\phpStudy\PHPTutorial\Apache\conf\httpd.conf
# 把注释去掉
更改后:LoadModule rewrite_module modules/mod_rewrite.so
二、设置访问权限 AllowOverride none改成All
三、入口文件 ,在网站public目录下新建.htaccess
//phpstudy的第一种写法
<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>
//第一种方法很差使的话 使用第二种方法
<IfModule mod_rewrite.c> Options +FollowSymlinks -Multiviews RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)$ index.php [L,E=PATH_INFO:$1] </IfModule>
四、重启服务
五、url地址变化
a、隐藏以前
http://www.tp5.com/index.php/Index/test
b、隐藏以后
http://www.tp5.com/Index/test
1.支持三种方式的url解析规则
2.路由只针对应用,不针对模块,所以路由的设置也是针对应用下面的全部模块。
3.若是有些模块不想使用路由 关闭后台模块,在后台入口文件添加下面这句话,写在
加载框架引导文件以后 不然报错
// 加载框架引导文件 require __DIR__ . '/../thinkphp/start.php';
//关闭admin模块下的路由 \think\App::route(false);
四、路由模式
a、普通模式
1.关闭路由,彻底使用默认的PATH_INFO方式URL
2.形式:http://www.tp5.com/admin.php/Index/index
3.如何配置:
//是否开启路由
‘url_route_on’ => false,
//是否强制使用路由
'url_route_must' => false,
b、混合模式
1.定义:开启路由,并使用路由定义+默认 PATH_INFO 方式的混合
2.如何设置
//是否开启路由
‘url_route_on’ => true,
//是否强制使用路由
'url_route_must' => false,
c、强制模式
1.定义:
开启路由,并设置必须定义路由才能访问
2.如何设置
//是否开启路由
‘url_route_on’ => true,
//是否强制使用路由
'url_route_must' =>true,
五、设置路由-动态单个注册
a、设置路由文件
application\route.php
b.、如何设置
//引入系统类 use think\Route; //定义路由规则
//设置了路由以后,就不能使用PATH_INFO模式访问了
//注册路由访问到index模块Index控制器index方法 Route::rule('/','index/Index/index');
//注册路由访问到index模块Index控制器test方法
Route::rule('test','index/Index/test');
c、路由的形式
1.静态地址路由
// 注册路由test 访问到Index模块index控制器test方法
Route::rule('test','index/Index/test');
2.给路由带参数
//注册带参数路由
//http://www.tp5.com/course/1 路由模式
//http://www.tp5.com/index/Index/course/id/1 普通模式
Route::rule('course/:id','index/Index/course');
//若是路由设置两个参数,必须带两个参数
Route::rule('time/:year/:mouth','index/Index/shijian');
3.可选参数路由
//http://www.tp5.com/time/2017
//http://www.tp5.com/time/2018/5
Route::rule('time/:year/[:mouth]','index/Index/shijian');
四、全动态路由(不建议使用)
Route::rule(':a/:b','index/Index/dongtai');
5.彻底匹配路由
//http://www.tp5.com/wanquan 能够访问
//http"//www.tp5.com/wanquan/ada/asf/a/f 不能够访问
Route::rule('wanquan$','index/Index/wanquan');
6.路由额外带参数
Route::rule('test1','index/Index/test1?id=12&name=adasfds');
d、设置请求类型
一、tp中请求类型
get、post、put、delete
二、Route::rule() 默认支持全部请求类型
三、设置全部请求
//支持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');
四、如何模拟put和delete请求
<form action="" method="post"> post 重要 <p> <input type="hidden" name="_method" value="PUT"> **隐藏域重要 <input type="text" name="name"> </p> <p> <input type="submit" value="提交"> </p> </form>
六、设置路由-动态批量注册
a、基本格式
Route::rule([ '路由规则1'=>'路由地址和参数', '路由规则2'=>['路由地址和参数','匹配参数(数组)','变量规则(数组)'] ... ],'','请求类型','匹配参数(数组)','变量规则');
b、使用
Route::rule([ 'test' => 'index/Index/test', 'course/:id' => 'index/Index/course' ]);
Route::get([ 'test' => 'index/Index/test', 'course/:id' => 'index/Index/course' ]);
七、设置路由-配置文件批量注册
return [ 'test' => 'index/Index/test', 'course/:id' => 'index/Index/course' ];
八、变量规则
//设置路由参数id必须是数字,必须1到3位
Route::rule('course/:id','index/Index/course','get',[],["id" => "\d{1,3}"]);
九、路由参数
//路由参数method 请求方式必须是get
//路由参数ext 主要设置路由的后缀
Route::rule('course/:id','index/Index/course','get',['method'=>'get','ext'=>'html'],["id" => "\d{1,3}"]);
十、资源路由
a、后台功能
add页面、展现页面、删除功能、修改页面、修改功能、增长功能
a、声明资源路由
Route::resource('blog','index/Blog');
b、会自动注册七个路由规则
十一、设置快捷路由
a、声明
Route::controller('user','index/User');
b、使用:
namespace app\index\controller; class User { public function getInfo() { } public function getPhone() { } public function postInfo() { } public function putInfo() { } public function deleteInfo() { } }
c、URL访问
get http://localhost/user/info get http://localhost/user/phone post http://localhost/user/info put http://localhost/user/info delete http://localhost/user/info
十二、生成URL地址
a、系统类
use think\url
url::build('index/Index/index');
b、系统方法
url('index/Index/index');
c、使用
看手册详细介绍