Code前端首页关于Code前端联系我们

thinkphp 6重要注意事项:多应用模式,中间件

terry 2年前 (2023-09-25) 阅读数 64 #后端开发

启用调试模式

根文件夹中的.example..env已更改为.env文件

使用立面。该类不需要实例化,但可以直接静态调用。

参见
注意,此时 IDE 无法识别门面。可以通过注解方式

/**
 * @method string hello(string $name)  static 读取缓存并删除
 */
class Test extends Facade{


来识别函数,静态调用系统函数,可以使用think\facade\xxx的命名空间

。 build.php文件会被放到app文件夹下并执行
php think build 应用名称

中间件

  1. 生成中间件
    php think make: middle正确 检查

2。设置中间件

class InAppCheck
{
    public function handle($request, \Closure $next)
    {
        if (preg_match('~micromessenger~i', $request->header('user-agent'))) {
            $request->InApp = 'WeChat';
        } else if (preg_match('~alipay~i', $request->header('user-agent'))) {
            $request->InApp = 'Alipay';
        }
        return $next($request);
    }
}

3。应用中间件

//在对应的 middleware.php

return [
    app\middleware\InAppCheck::class,
];

或路由中间件

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

路由

变量规则

调整默认规则
'default_route_pattern' = > '[\w\-]+', 本地变量

Route::get('new/:name', 'News/read')
    ->pattern(['name' => '[\w|\-]+']);

全局变量规则

Route::pattern([
    'name' => '\w+',
    'id'   => '\d+',
]);

验证器的推荐使用方式

使用显示路径格式以方便 IDE 跟踪

try{
    $data = $this->request->post();
    validate(\app\api\validate\ChildrenValidate::class)
        ->scene("binding")
        ->check($data);
        // todo...
}catch(ValidateException|CustomException $e){
	$this->error($e->getError());
}

版权声明

本文仅代表作者观点,不代表Code前端网立场。
本文系作者Code前端网发表,如需转载,请注明页面地址。

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

热门