tp5總結(二)


1.簡化路由【一方面可以更好的讓搜索引擎抓取;另一方面簡化路由,利於記憶】

1-1.在【public/】下面創建admin.php 復制index.php 然后綁定前后台模塊define('BIND_MODEL','admin')和define('BIND_MODEL',index')

eg:http://ww:7070/tp5-2/public/admin和http://ww:7070/tp5-2/public/index

1-2. 隱藏入口文件

開啟Apache的配置文件,將LoadModule rewrite_module modules/mod_rewrite.so前面的注釋去掉

eg:http://ww:7070/tp5-2/public/index/index

1-3.隱藏public,將index.php入口文件放在根目錄下,並且修改相對路徑

2.路由

2-1.關閉后台模塊的路由:public/admin后面寫 【\think\app:route(false)】

2-2.路由模式,修改config.php  [url_route_on和url_route_must]

2-3.修改route.php來修改路由

2-3-1.靜態路由 eg:http://ww:7070/tp5-2/ 

use \think\Route;
Route::rule('/','admin/index/index'); 

2-3-2.動態靜態組合

2-3-3.所有路由

<?php
//配置文件注冊方式
//return [
//    '__pattern__' => [
//        'name' => '\w+',
//    ],
//    '[hello]'     => [
//        ':id'   => ['Index/hello', ['method' => 'get'], ['id' => '\d+']],
//        ':name' => ['Index/hello', ['method' => 'post']],
//    ],
//
//];
use think\Route;
Route::rule('/','Index/Index/index1');
Route::rule('canshu/:id','Index/Index/canshu');//帶一個參數
Route::rule('time/:year/:month','Index/Index/time');//帶兩個個參數
Route::rule('kexuan/:year/[:month]','Index/Index/kexuan');//帶兩可選參數
Route::rule(':a/:b','Index/Index/dongtai');//全動態路由[不建議用]
Route::rule('wanquan$','Index/Index/wanquan');//全動態路由[不建議用]


//設定路由類型
//


Route::rule('type','Index/Index/type','post|get');//即支持get又2支持post

//支持所有

//Route::rule('type','Index/Index/type','*');//支持所有
//Route::any('type','Index/Index/type');//支持所有

//put請求
//Route::rule('type','Index/Index/type','put');


//批量注冊
//Route::rule([
//    'd1'=>'admin/index/test1',
//    'd2'=>'admin/index/test2'
//],'','get');
Route::get([
    'd1'=>'admin/index/test1',
    'd2'=>'admin/index/test2'
]);


//路由規則
Route::rule('d3/:id','admin/index/test3','get',[],['id'=>'\s+']);//參數必須是數字
//Route::rule('d3/:id','admin/index/test3',[],['id'=>'\d{1,3}']);//參數必須是數字1-3位

//資源路由 會默認注冊七個路由規則  index
Route::resource('myblog','index/myblog');

//快捷路由
//Route::controller('myblog','index/myblog');

3.路由地址生成 eg:http://ww:7070/tp5-2/public/myblog


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM