yii-goaop-将goaop/framework集成到Yii中,Yii中优雅的面向方面的编程。项目地址https://github.com/guanguans/yii-goaop环境要求Yii>=2.0安装$composerrequireguanguans/yii-goaop-vvvcomposer.jsonadd:"autoload":{"psr-4":{"backend\\":"backend/","frontend\\":"frontend/","common\\":"common/","console\\":"console/","app\\":""}}$composerdumpautoload配置yii2-app-advanced配置config/main.php添加:['aop',],'components'=>['aop'=>['class'=>'Guangans\YiiGoAop\GoAopComponent','initOption'=>[//AOP调试模式'debug'=>false,//应用根目录'appDir'=>dirname(dirname(__DIR__)),//AOP缓存Directory'cacheDir'=>dirname(__DIR__).'/runtime/aspect',//缓存文件模式'cacheFileMode'=>511,//杂项AOP引擎特性'features'=>0,//目录白名单'includePaths'=>[dirname(__DIR__),],//目录黑名单'excludePaths'=>[dirname(__DIR__).'/runtime',dirname(__DIR__).'/tests',dirname(__DIR__).'/views',],//AOP容器'containerClass'=>\Go\Core\GoAspectContainer::class,],//你的aspects'aspects'=>[frontend\aspects\LoggingAspect::class,],],]];yii2-app-basic配置config/web.php文件中添加:return['bootstrap'=>['aop',],'components'=>['aop'=>['class'=>'关关\YiiGoAop\GoAopComponent','initOption'=>[//AOP调试模式'debug'=>false,//应用根目录'appDir'=>dirname(dirname(__DIR__)),//AOP缓存目录'cacheDir'=>dirname(__DIR__).'/runtime/aspect',//缓存文件模式'cacheFileMode'=>511,//杂项AOP引擎特性'features'=>0,//目录白名单'includePaths'=>[dirname(__DIR__).'/assets',dirname(__DIR__).'/aspects',dirname(__DIR__).'/commands',dirname(__DIR__).'/controllers',dirname(__DIR__).'/models',dirname(__DIR__)).'/widgets',],//目录黑名单'excludePaths'=>[dirname(__DIR__).'/config',dirname(__DIR__).'/mail',dirname(__DIR__).'/runtime',dirname(__DIR__).'/tests',dirname(__DIR__).'/vagrant',dirname(__DIR__).'/vendor',dirname(__DIR__).'/views',dirname(__DIR__).'/web',],//AOP容器'containerClass'=>\Go\Core\GoAspectContainer::class,],//你的方面'aspects'=>[app\aspects\LoggingAspect::class,],],]];使用示例创建切面publicfrontend\controllers\SiteController->*Index(*)*Index(*))")*/publicfunctionbeforeMethodExecution(MethodInvocation$invocation){file_put_contents(Yii::$app->getRuntimePath().'/logs/logging.log','thisisabeforemethodtesting.'.PHP_EOL,FILE_APPEND);}/***将在实际方法之后调用的方法*@paramMethodInvocation$invocationInvocation*@After("execution(publicfrontend\controllers\SiteController->*Index(*))")*/publicfunctionafterMethodExecution(MethodInvocation$invocation){file_put_contents(Yii::$app->getRuntimePath().'/logs/logging.log','thisisaaftermethodtesting.'.PHP_EOL,FILE_APPEND);}}运行访问http://localhost:8888/index.php?r=site/indexcatfrontend/runtime/logs/logging.log────────┬────────────────────────────────────────────────────────────────────│文件:frontend/runtime/logs/logging.log────────┼─────────────────────────────────────────────────────────────────1│这是一个before方法测试。2│这是一个后方法测试。────────┴────────────────────────────────────────────────────────────────────关联链接https://github.com/goaop/framework
