标签:basic nts UNC params nis 执行方法 dump mat handle
官网文档 https://www.swoft.org/documents/v2/basic-components/aop/
视频教程 https://www.bilibili.com/video/BV12J411j721?p=51
切面 我理解 就是不破坏原有代码逻辑,往里面加入需要执行的前置 后置 操作
include
:需被 指定 为切点的目标类集合exclude
:需被 排除 为切点的目标类集合inlucde
:需被 织入 的注解类集合exclude
:需被 排除 的注解类集合include
:需被 织入 的目标类方法集合,支持正则表达式exclude
:需被 排除 的目标类方法集合,支持正则表达式定义切面类 app\Aspect\TestAspect.php
指定方法切入的
<?php namespace App\Aspect; use Swoft\Aop\Annotation\Mapping\After; use Swoft\Aop\Annotation\Mapping\Aspect; use Swoft\Aop\Annotation\Mapping\Before; use Swoft\Aop\Annotation\Mapping\PointExecution; /** * @Aspect() * @PointExecution(include={"App\Http\Controller\HomeController::hi.*"}) */ class TestAspect { /** * @Before() */ public function before() { echo "我是前置".PHP_EOL; } /** * @After() */ public function after() { echo "我是后置".PHP_EOL; } }
浏览器访问 /hi
控制台输出
____ _____ ___ ___ / __/ _____ / _/ /_ |_ | / _ _\ \| |/|/ / _ \/ _/ __/ / __/_/ // / /___/|__,__/\___/_/ \__/ /____(_)___/ SERVER INFORMATION(v2.0.9) ******************************************************************************** * HTTP | Listen: 0.0.0.0:18306, Mode: Process, Worker: 6, Task worker: 12 ******************************************************************************** HTTP Server Start Success! 2020/07/18-19:18:38 [INFO] Swoft\Server\Server:startSwoole(491) Swoole\Runtime::enableCoroutine 2020/07/18-19:18:38 [INFO] Swoft\Listener\BeforeStartListener:handle(27) Server extra info: pidFile @runtime/swoft.pid 2020/07/18-19:18:38 [INFO] Swoft\Listener\BeforeStartListener:handle(28) Registered swoole events: start, shutdown, managerStart, managerStop, workerStart, workerStop, workerError, request, task, finish Server start success (Master PID: 16060, Manager PID: 16065) 我是前置 我是后置
访问hello 控制台不会触发 不会打印
如果改为
/**
* @Aspect()
* @PointExecution(include={"App\Http\Controller\HomeController::h.*"})
*/
就会触发了
通过使用连接点 joinpoint可以拿到执行方法的参数 返回值等 可以放到前置 做鉴权 后置 做日志
<?php namespace App\Aspect; use Swoft\Aop\Annotation\Mapping\After; use Swoft\Aop\Annotation\Mapping\AfterReturning; use Swoft\Aop\Annotation\Mapping\Aspect; use Swoft\Aop\Annotation\Mapping\Before; use Swoft\Aop\Annotation\Mapping\PointExecution; use Swoft\Aop\Point\JoinPoint; use Swoft\Http\Message\Request; /** * @Aspect() * @PointExecution(include={"App\Http\Controller\HomeController::h.*"}) */ class TestAspect { /** * @Before() * @param JoinPoint $joinPoint */ public function before(JoinPoint $joinPoint) { echo "我是前置".PHP_EOL; /** @var Request $request */ $request = $joinPoint->getArgs()[0]; $params = $request->getQueryParams(); //可以用来做鉴权 var_dump($params); } /** * @After() */ public function after() { echo "我是后置".PHP_EOL; } /** * @AfterReturning() * @param JoinPoint $joinPoint * @param $request Request */ public function afterrunning(JoinPoint $joinPoint) // 可以日志记录 { echo "程序执行完了".PHP_EOL; // /** @var Request $request */ // $request = $joinPoint->getArgs()[0]; // $params = $request->getBody(); // var_dump($params); return $joinPoint->getReturn(); } }
访问浏览器 结果如下
HTTP Server Start Success! 2020/07/18-19:42:54 [INFO] Swoft\Server\Server:startSwoole(491) Swoole\Runtime::enableCoroutine 2020/07/18-19:42:54 [INFO] Swoft\Listener\BeforeStartListener:handle(27) Server extra info: pidFile @runtime/swoft.pid 2020/07/18-19:42:54 [INFO] Swoft\Listener\BeforeStartListener:handle(28) Registered swoole events: start, shutdown, managerStart, managerStop, workerStart, workerStop, workerError, request, task, finish Server start success (Master PID: 16459, Manager PID: 16464) 我是前置 array(1) { ["id"]=> string(1) "4" } 我是后置 程序执行完了
标签:basic nts UNC params nis 执行方法 dump mat handle
原文地址:https://www.cnblogs.com/brady-wang/p/13337069.html