Getting Started with thinkphp6 (9) -- Get the application name, controller name, and operation name in the url path

To determine the user's operation permissions, we may need to obtain the application name, controller name, and operation name in the current URL path.

If you use multi-application mode, you can get the current application through the following method

app('http')->getName();

Get the current controller

Request::controller();

Get current operation

Request::action();

Controllers and operations cannot be obtained in middleware

The introduction of middleware needs to be modified to route.php in the config directory.

<?php
return [
    'middleware' => [
        // 系统日志记录
        //    \app\run\middleware\SystemLog::class,
        // 检测登录状态
        \app\middleware\AuthMiddleware::class,
    ],
];

Get the application name, controller name, and operation name in middleware

<?php

namespace app\middleware;

use think\facade\Request;
use think\facade\Session;

// 前置行为的中间件
class AuthMiddleware
{
    public function handle($request, \Closure $next)
    {
        // 添加中间件执行代码 start

        // 获取当前应用名
        $app = app('http')->getName();
        // 获取当前控制器名
        $controller = Request::controller('true');
        // 获取当前操作名
        $action = Request::action('true');

        // echo $app;
        // echo "4444<br/>";
        // echo $controller;
        // echo "4444<br/>";
        // echo $action;
        // echo "4444<br/>";

        if (!$app || !$controller || !$action)
        {
            echo "未能获取到app,controller,action";
            exit();
        }



        // 校验权限的代码



        // 添加中间件执行代码 end

        return $next($request);
    }
}

Request information·ThinkPHP6.0 complete development manual·Look at the cloudThinkPHP`6.0` is based on the two principles of streamlined core and unified usage, and the underlying architecture is improved on the basis of`5.1` Further optimization and improvements have been made and become more standardized. icon-default.png?t=N7T8https://www.kancloud.cn/manual/thinkphp6_0/1037518

Software engineering student Xiao Shi

20231013

Guess you like

Origin blog.csdn.net/u013288190/article/details/133817678