thinkphp底層原理速成:入口文件、路由模式、路由設置和url生成
本文詳細介紹了ThinkPHP5.0的路由功能,包括路由的作用、入口文件配置、路由模式(普通、混合、強制)、路由設置方法(動態(tài)單個注冊、動態(tài)批量注冊、配置文件批量注冊)、變量規(guī)則、路由參數(shù)、資源路由的聲明和自動注冊規(guī)則,以及快捷路由的聲明和控制器使用。此外,還講解了如何生成URL以及隱藏入口文件的設置。
一、路由的作用
簡化URL地址,方便記憶有利于搜索引擎的優(yōu)化
二、入口文件
前后臺分離
在網(wǎng)站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';
綁定模塊
實現(xiàn)功能
index.php 這個入口文件,只能去前臺模塊
admin.php這個入口文件,只能去后臺模塊(建議后臺入口文件復雜一些)
如何實現(xiàn)
在入口文件中
// 定義前臺
define('BIND_MODULE', 'index');
// 綁定后臺
define('BIND_MODULE', 'admin');URL地址發(fā)生變化
入口綁定之前(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>入口文件,在網(wǎng)站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解析規(guī)則路由只針對應用,不針對模塊,因此路由的設置也是針對應用下的所有模塊。
關閉后臺模塊,在后臺入口文件中(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.動態(tài)單個注冊
設置路由格式
Route::rule(‘路由表達式’, ‘路由地址’, ‘請求類型’, ‘路由參數(shù)(數(shù)組)’, ‘變量規(guī)則(數(shù)組)’)
設置路由文件(項目\application\route.php)
如何設置(route.php)
use think\Route;
// 定義路由規(guī)則
// 設置路由之后,就不能使用pathinfo訪問了
Route::rule('/','index/index/index');
//注冊路由訪問到index模塊下的index控制器下的index的方法
Route::rule('test','index/index/test');
//注冊路由test 訪問到index模塊下的index控制器下的test的方法路由的形式
1、靜態(tài)地址路由
//注冊路由test 訪問到index模塊下的index控制器下的test的方法
Route::rule('test','index/index/test');2、給路由帶參數(shù)
route.php中
//注冊帶參數(shù)路由
//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、給路由帶多個參數(shù)(設置了帶幾個就必需帶幾個)
route.php中
//注冊帶參數(shù)路由
//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、可選參數(shù)路由
route.php中
//注冊帶可選參數(shù)路由
//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、全動態(tài)路由(不建議使用)
route.php中
//注冊帶可選參數(shù)路由
//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中
//注冊帶可選參數(shù)路由
//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、帶額外參數(shù)
route.php中
// 帶額外參數(shù)
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.設置路由-動態(tài)批量注冊
1.基本格式
Route::rule([
'路由規(guī)則1'=>'路由地址和參數(shù)',
'路由規(guī)則2'=>['路由地址和參數(shù)','匹配參數(shù)(數(shù)組)','變量規(guī)則(數(shù)組)'],
...
],'','請求類型','匹配參數(shù)(數(shù)組)','變量規(guī)則');2.使用
// 動態(tài)批量注冊
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"
];
五、變量規(guī)則
Route::rule(‘路由表達式’,’路由地址’,’請求類型’,’路由參數(shù)(數(shù)組)’,’變量規(guī)則(數(shù)組)’);
// ['id'=>'\d{1,3}','name'=>'\w+']設置路由變量規(guī)則,id只能是1-3位數(shù)字,name只能是hi字符串
Route::rule("course/:id/:name","index/index/course",'get',[],['id'=>'\d{1,3}','name'=>'\w+']);六、路由參數(shù)
路由參數(shù)是指可以設置一些路由匹配的條件參數(shù),主要用于驗證當前的路由規(guī)則是否有效,主要包括:
Route::rule('course/:id/:name','index/index/course','get',['method'=>'get','ext'=>'html'],['id'=>'\d{1,3}','name'=>'\w+']);
// 路由參數(shù)method 請求方式必需是get
// 路由參數(shù)ext 主要設置路由的后綴
參數(shù) 說明
method 請求類型檢測,支持多個請求類型
ext URL后綴檢測,支持匹配多個后綴
deny_ext URL禁止后綴檢測,支持匹配多個后綴
https 檢測是否https請求
domain 域名檢測
before_behavior 前置行為(檢測)
after_behavior 后置行為(執(zhí)行)
callback 自定義檢測方法
merge_extra_vars 合并額外參數(shù)
bind_model 綁定模型(V5.0.1+)
cache 請求緩存(V5.0.1+)
param_depr 路由參數(shù)分隔符(V5.0.2+)
ajax Ajax檢測(V5.0.2+)
pjax Pjax檢測(V5.0.2+)七、資源路由
1.聲明
Route::resource(‘blog’,’index/blog’);
也可以在定義資源路由的時候限定執(zhí)行的方法(標識),例如:
Route::resource('blog','index/blog',['only'=>['index','read','edit','update']]);
Route::resource('blog','index/blog',['except'=>['index','delete']]);2.會動注冊7個路由規(guī)則(一定要記憶)
| 標識 | 請求類型 | 生成路由規(guī)則 | 對應操作方法(默認) |
|---|---|---|---|
| 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.聲明
// 聲明快捷路由
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.系統(tǒng)類
dump(Url::build('index/index/index'));
2.系統(tǒng)方法
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底層原理速成:入口文件、路由模式、路由設置和url生成的文章就介紹到這了,更多相關thinkphp原理:入口文件、路由模式、路由設置和url生成內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
php+mysql查詢實現(xiàn)無限下級分類樹輸出示例
這篇文章主要介紹了php+mysql查詢實現(xiàn)無限下級分類樹輸出,結合實例形式分析了php+MySQL查詢實現(xiàn)的樹狀分類輸出功能,涉及php數(shù)據(jù)庫查詢與數(shù)組遍歷等相關操作技巧,需要的朋友可以參考下2016-10-10
php程序內(nèi)部post數(shù)據(jù)的方法
php自動獲取字符串編碼函數(shù)mb_detect_encoding
Array of country list in PHP with Zend Framework

