thinkphp 路由學習筆記


路由 (Route::rule('路由規則','路由地址','請求類型','路由參數(數組)','變量規則(數組)')😉

在 應用層下,創建或者打開route.php文件
use think\Route;  //導入Route類
Route::rule('add','index/index/add/');  //創建路由規則

對應的地址將是 127.0.0.1/tp5/public/add

// 限制只能讀取id值
Route::rule(['add','add/:id'],'index/index/add/');  //創建路由規則 

Route::rule(['add','add/:student/[:sid]'],'index/index/add/');  // sid 可選

批量注冊路由

Route::rule([
	'路由規則1'=>'路由地址和參數',
	'路由規則2'=>['路由地址和參數','匹配參數(數組)','變量規則(數組)']
	...
],'','請求類型','匹配參數(數組)','變量規則');
---------------------------------------------------
Route::rule([
	'getinfo'  =>  ['index/index/getinfo', ['ext'=>'shtml']],
	'blog/:id' =>  ['Blog/update',['id'=>'\d{4}']],
],'','GET',['ext'=>'html'],['id'=>'\d+']);

getinfo 解析到 index/index/getinfo 模塊下
ext 代表 只解析 域名后綴為shtml的地址
第二個 ['ext'=>'html'] 表示,在其他路由沒有定義的情況下,默認使用html后綴的解析

return 形式配置路由

return [
	'new/:id'   => 'News/read',
	'blog/:id'   => ['Blog/update',['method' => 'post|put'], ['id' => '\d+']],
]

變量規則定義

  • 全局規則

    // 設置name變量規則(采用正則定義)
    Route::pattern('name','\w+');
    // 支持批量添加
    Route::pattern([
    'name' => '\w+',
    'id' => '\d+',
    ]);

  • 局部規則

    / 定義GET請求路由規則 並設置name變量規則
    Route::get('new/:name','News/read',[],['name'=>'\w+']);

    可參考批量注冊路由項

// 設置路由配置文件列表

'route_config_file'	=>	['home','admin'],
application/home.php  配置home模塊的路由規則,
application/admin.php 則配置admin模塊的路由規則。

查看路由規則

// 獲取全部的路由信息
Route::rules();
// 獲取GET路由信息
Route::rules('GET');

緩存路由

	緩存路由很簡單,到操作系統的命令行下面,在應用根目錄下面執行命令:

	php think optimize:route

緩存路由

Route::group('show',function(){
	Route::get('','index/Index/hello');
	Route::post('','index/Index/insert');
	Route::put('','index/Index/read');
	Route::delete('','index/Index/del');
});

Route::group('分組名或分組參數','分組路由規則','公共路由參數','公共變量規則');

別名路由

Route::alias('路由別名','別名地址(控制器或類)','路由參數(數組)');
https://www.kancloud.cn/thinkphp/route-master/223114

快速生成Restful風格控制器

(進入根目錄)php think make:controller index/Blog
自動生成

<?php

namespace app\index\controller;

use think\Controller;
use think\Request;

class Blog extends Controller
{
/**
 * 顯示資源列表
 *
 * @return \think\Response
 */
public function index()
{
    //
    return 'get';
}

/**
 * 顯示創建資源表單頁.
 *
 * @return \think\Response
 */
public function create()
{
    //
    return 'create';
}
	*********

}

如果使用路由注冊上面的方法的話

  • 方法一:

    Route::get('blogs','index/Blog/index');
    Route::get('blogs/create','index/Blog/create');
    Route::post('blogs','index/Blog/save');
    Route::get('blogs/:id','index/Blog/read');
    Route::get('blogs/:id/edit','index/Blog/edit');
    Route::put('blogs/:id','index/Blog/update');
    Route::delete('blogs/:id','index/Blog/delete');

  • 方法二:

    Route::group(['name'=>'blogs','prefix'=>'index/Blog'], function() {
    Route::get('/','index');
    Route::get('create','create');
    Route::post('/','save');
    Route::get(':id','read');
    Route::get(':id/edit','edit');
    Route::put(':id','update');
    Route::delete(':id','delete');
    });

  • 方法三(簡便):

    Route::resource('blogs','index/Blog');

  • 方法四(簡便):

    return [
    // 定義資源路由
    'rest' => [
    'blogs' => 'index/Blog',
    ],
    ]

https://www.kancloud.cn/thinkphp/route-master/223115

miss路由 (當為匹配到路由時候的規則)

Route::miss(function(){
	return '請求錯誤';
});

AUTO路由 (在開啟了強制路由的時候,如果沒有任何路由匹配則自動解析相應的路由規則)

Route::group('blog',function(){
	Route::auto('index');
	Route::rule(':id','blog/read',[],['id'=>'\d+']);
	Route::rule(':name','blog/read',[],['name'=>'\w+']);
},['method'=>'get','ext'=>'html']);

學習地址 https://www.kancloud.cn/thinkphp/route-master


免責聲明!

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



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