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);
}
}
Software engineering student Xiao Shi
20231013