在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
AOP实践: 2、执行 composer require goaop/framework 3、修改composer.json文件,加入如下代码段: "autoload": { "psr-4": { "backend\\": "backend//", "frontend\\": "frontend//", "common\\": "common//" } } 4、 在frontend 目录下创建一个components是目录,并新建一个类AopAspectKernel,例如: namespace frontend\components; use frontend\aspects\MonitorAspect; use Go\Core\AspectContainer; use Go\Core\AspectKernel; class AopAspectKernel extends AspectKernel { protected function configureAop(AspectContainer $container) { $container->registerAspect(new MonitorAspect()); } } 5、在forntend目录下在新建一个类InitAopComponent,并使其实现BootstrapInterface,使其可以在YII2框架引导时被自动引导 namespace frontend\components; use yii\base\BootstrapInterface; class InitAopComponent implements BootstrapInterface { public function bootstrap($app) { print_r(\Yii::$app->params['aop']); $applicationAspectKernel = AopAspectKernel::getInstance(); $applicationAspectKernel->init(\Yii::$app->params['aop']); } } 6、在frontend/config/params.php中新增如下代码: 'aop' => [ 'debug' => true, 'appDir' => dirname(__DIR__), 'cacheDir' => dirname(__DIR__) . '/runtime/aop', 'includePaths' => [ dirname(__DIR__) ] ] 7、在frontend下面新建aspects目录,并新建类MonitorAspect,代码如下: namespace frontend\aspects; use Go\Aop\Aspect; use Go\Aop\Intercept\MethodInvocation; use Go\Lang\Annotation\Before; class MonitorAspect implements Aspect { /** * Method that will be called before real method * * @param MethodInvocation $invocation Invocation * @Before("execution(public frontend\components\AopTestComponent->*(*))") */ public function beforeMethodExecution(MethodInvocation $invocation) { $obj = $invocation->getThis(); echo 'Calling Before Interceptor for method: ', is_object($obj) ? get_class($obj) : $obj, $invocation->getMethod()->isStatic() ? '::' : '->', $invocation->getMethod()->getName(), '()', ' with arguments: ', json_encode($invocation->getArguments()), "<br>\n"; } } 8、修改frontend/config/main.php文件,并在components数组下新增一个key,代码如下: 'components'=>[ 'aop' => [ 'class' => 'frontend\components\InitAopComponent' ] ] 9、修改frontend/config/main.php文件,并在bootstrap数组下新增aop值,代码如下: 'bootstrap'=>['log','aop'] 原文链接:https://www.cnblogs.com/cmacro/p/9327602.html 参考链接:https://blog.csdn.net/guiyecheng/article/details/56016791 |
请发表评论