转载请著名来源,侵权必究
//属性接口
declare(strict_types=1);
namespace app\common\attribute;
interface AttributeHandler
{
public function handler(mixed $handler);
}
//不需要登陆属性
declare(strict_types=1);
namespace app\common\attribute;
/**
* 跳过登陆验证
*/
#[\Attribute(\Attribute::TARGET_METHOD)]
class NoLogin implements AttributeHandler
{
public function __construct(){
}
public function handler(mixed $handler)
{
// TODO: Implement handler() method.
//return Response::create('页面未找到 - NoLogin', 'html', 404);
}
}
//权限验证属性
declare(strict_types=1);
namespace app\shop\attribute;
use app\common\attribute\AttributeHandler;
/**
* 权限验证
*/
#[\Attribute(\Attribute::TARGET_METHOD)]
class CheckPermission implements AttributeHandler
{
public function __construct(private string $permission){
}
public function handler(mixed $handler)
{
// TODO: Implement handler() method.
//return Response::create('页面未找到 - CheckPermission', 'html', 404);
}
}
注意这里是要加到 控制器中间件
//属性中间件 (控制器中间件)
declare(strict_types=1);
namespace app\common\middleware;
use app\common\attribute\AttributeHandler;
use Closure;
use think\App;
use think\Request;
use think\Response;
use think\Session;
/**
* 属性中间件
*/
class AttributeMiddleware
{
public function __construct(protected App $app, protected Session $session)
{
}
public function handle(Request $request, Closure $next): Response
{
/** @var Response $response */
$response = null;
$reflect = new \ReflectionClass('app\\'.app('http')->getName().'\controller\\'.$request->controller());
$attributes = $reflect->getMethod($request->action())->getAttributes();
foreach($attributes as $attribute){
/**
* @var AttributeHandler $instance
* **/
$instance = $attribute->newInstance();
$response = $instance->handler($this);
if($response instanceof Response){
return $response;
}
}
$request->attributes = $attributes;
$response = $next($request);
return $response;
}
}
//测试类
<?php
namespace app\shop\controller;
use app\common\attribute\NoLogin;
use app\shop\attribute\CheckPermission;
use app\shop\BaseController;
class Index extends BaseController
{
#[CheckPermission(permission:"Foo")]
public function index()
{
return '<style>*{ padding: 0; margin: 0; }</style><iframe src="https://www.thinkphp.cn/welcome?version=' . \think\facade\App::version() . '" width="100%" height="100%" frameborder="0" scrolling="auto"></iframe>';
}
#[NoLogin]
public function hello($name = 'ThinkPHP8')
{
return 'hello,' . $name;
}
}
标签:function,Thinkphp8,use,app,中间件,response,handler,AOP,public
From: https://www.cnblogs.com/kuangke/p/18357295