thinkphp6-路由

2021/10/15 20:17:51

本文主要是介绍thinkphp6-路由,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

配置路由

路由配置文件 /route/app.php

<?php
use think\facade\Route;

Route::rule('index/create', 'index/create');
Route::rule('index/read', 'index/read');
Route::rule('index/update', 'index/update');
Route::rule('index/delete', 'index/delete');

控制器 /app/controller/Index.php

<?php
namespace app\controller;

use app\BaseController;

class Index extends BaseController
{
    public function index()
    {
        return 'hello,welcome to thinkphp6!';
    }

    public function create(){
        return 'create';
    }

    public function read(){
        return 'read';
    }

    public function update(){
        return 'update';
    }

    public function delete(){
        return 'delete';
    }
}

测试url

http://127.0.0.1:8000/index
http://127.0.0.1:8000/index/create
http://127.0.0.1:8000/index/read
http://127.0.0.1:8000/index/update
http://127.0.0.1:8000/index/delete

路由定义

Route::rule('new/:id','News/read','POST');
Route::get('new/<id>','News/read'); // 定义GET请求路由规则
Route::post('new/<id>','News/update'); // 定义POST请求路由规则
Route::put('new/:id','News/update'); // 定义PUT请求路由规则
Route::delete('new/:id','News/delete'); // 定义DELETE请求路由规则
Route::any('new/:id','News/read'); // 所有请求都支持的路由规则
Route::rule('/', 'index'); // 首页访问路由
Route::rule('my', 'Member/myinfo'); // 静态地址路由
Route::rule('blog/:id', 'Blog/read'); // 静态地址和动态地址结合
Route::rule('new/:year/:month/:day', 'News/read'); // 静态地址和动态地址结合
Route::rule(':user/:blog_id', 'Blog/read'); // 全动态地址
Route::get('blog/:year/[:month]','Blog/archive');
Route::get('new/:cate$', 'News/category');
Route::redirect('blog/:id', 'http://blog.thinkphp.cn/read/:id', 302);

变量规则

// 定义GET请求路由规则 并设置name变量规则
Route::get('new/:name', 'News/read')
    ->pattern(['name' => '[\w|\-]+']);
// 支持批量添加
Route::pattern([
    'name' => '\w+',
    'id'   => '\d+',
]);

路由参数

参数    说明    方法名
ext    URL后缀检测,支持匹配多个后缀    ext
deny_ext    URL禁止后缀检测,支持匹配多个后缀    denyExt
https    检测是否https请求    https
domain    域名检测    domain
complete_match    是否完整匹配路由    completeMatch
model    绑定模型    model
cache    请求缓存    cache
ajax    Ajax检测    ajax
pjax    Pjax检测    pjax
json    JSON检测    json
validate    绑定验证器类进行数据验证    validate
append    追加额外的参数    append
middleware    注册路由中间件    middleware
filter    请求变量过滤    filter

用法:

Route::get('new/:id', 'News/read')
    ->ext('html')
    ->https();
Route::get('new/:id', 'News/read')
    ->option([
        'ext'   => 'html',
        'https' => true
    ]);
Route::get('new/:name$', 'News/read')
    ->cache(3600);

路由中间件

Route::rule('hello/:name','hello')
    ->middleware(\app\middleware\Auth::class);
Route::rule('hello/:name','hello')
    ->middleware([\app\middleware\Auth::class,\app\middleware\Check::class]);

全局 config/route.php

'middleware'    =>    [
    app\middleware\Auth::class,
    app\middleware\Check::class,
],

miss路由

Route::miss('public/miss');
Route::miss(function() {
    return '404 Not Found!';
});

跨域请求

Route::get('new/:id', 'News/read')
    ->ext('html')
    ->allowCrossDomain();
Route::get('new/:id', 'News/read')
    ->ext('html')
    ->allowCrossDomain([
        'Access-Control-Allow-Origin'        => 'thinkphp.cn',
        'Access-Control-Allow-Credentials'   => 'true',
        'Access-Control-Max-Age'             => 600,
    ]);

URL生成

$url = \think\facade\Route::buildUrl();
$url = \think\facade\Route::buildUrl('index/read', ['id' => 5]);


这篇关于thinkphp6-路由的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程