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

猜你喜欢

转载自www.cnblogs.com/ar13/p/9061991.html