Yii-goaop – Integrate GoAOP/Framework into YII for elegant section-oriented programming in YII.

The project address

  • Github.com/guanguans/y…

Environmental requirements

  • Yii > = 2.0

The installation

$ composer require guanguans/yii-goaop -vvv
Copy the code

composer.json add:

"autoload": {
    "psr-4": {
        "backend\\": "backend/"."frontend\\": "frontend/"."common\\": "common/"."console\\": "console/"."app\\": ""}}Copy the code
$ composer dumpautoload
Copy the code

configuration

yii2-app-advanced

Add to config/main.php:


      

return [
    'bootstrap'= > ['aop',].'components'= > ['aop'= > ['class'= >'Guanguans\YiiGoAop\GoAopComponent'.'initOption'= > [// AOP Debug Mode
                'debug'= >false.// Application Root Directory
                'appDir'         => dirname(dirname(__DIR__)),
                // AOP Cache Directory
                'cacheDir'       => dirname(__DIR__).'/runtime/aspect'.// Cache File Mode
                'cacheFileMode'= >511.// Miscellaneous AOP Engine Features
                'features'= >0.// Directories White List
                'includePaths'   => [
                    dirname(__DIR__)],// Directories Black List
                'excludePaths'   => [
                    dirname(__DIR__).'/runtime',
                    dirname(__DIR__).'/tests',
                    dirname(__DIR__).'/views',].// AOP Container
                'containerClass' => \Go\Core\GoAspectContainer::class,
            ],
            // Yours aspects
            'aspects' => [
                frontend\aspects\LoggingAspect::class,
            ],
        ],
    ]
];
Copy the code

yii2-app-basic

Add to config/web.php:


return [
    'bootstrap'= > ['aop',].'components'= > ['aop'= > ['class'= >'Guanguans\YiiGoAop\GoAopComponent'.'initOption'= > [// AOP Debug Mode
                'debug'= >false.// Application Root Directory
                'appDir'         => dirname(dirname(__DIR__)),
                // AOP Cache Directory
                'cacheDir'       => dirname(__DIR__).'/runtime/aspect'.// Cache File Mode
                'cacheFileMode'= >511.// Miscellaneous AOP Engine Features
                'features'= >0.// Directories White List
                'includePaths'   => [
                    dirname(__DIR__).'/assets',
                    dirname(__DIR__).'/aspects',
                    dirname(__DIR__).'/commands',
                    dirname(__DIR__).'/controllers',
                    dirname(__DIR__).'/models',
                    dirname(__DIR__).'/widgets',].// Directories Black List
                '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 Container
                'containerClass' => \Go\Core\GoAspectContainer::class,
            ],
            // Yours aspects
            'aspects' => [
                app\aspects\LoggingAspect::class,
            ],
        ],
    ]
];
Copy the code

Use the sample

Create a planepublic frontend\controllers\SiteController->*Index(*)


      

namespace frontend\aspects;

use Go\Aop\Aspect;
use Go\Aop\Intercept\MethodInvocation;
use Go\Lang\Annotation\Before;
use Go\Lang\Annotation\After;
use Yii;

class LoggingAspect implements Aspect
{
    /**
     * Method that will be called before real method
     * @param  MethodInvocation  $invocation  Invocation
     * @Before("execution(public frontend\controllers\SiteController->*Index(*))")
     */
    public function beforeMethodExecution(MethodInvocation $invocation)
    {
        file_put_contents(Yii::$app->getRuntimePath().'/logs/logging.log'.'this is a before method testing.'.PHP_EOL, FILE_APPEND);
    }

    /**
     * Method that will be called after real method
     * @param  MethodInvocation  $invocation  Invocation
     * @After("execution(public frontend\controllers\SiteController->*Index(*))")
     */
    public function afterMethodExecution(MethodInvocation $invocation)
    {
        file_put_contents(Yii::$app->getRuntimePath().'/logs/logging.log'.'this is a after method testing.'.PHP_EOL, FILE_APPEND); }}Copy the code

Run the accesshttp://localhost:8888/index.php?r=site/index

cat frontend/runtime/logs/logging.log

─ ─ ─ ─ ─ ─ ─ ┬ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ │ File: Frontend/runtime/logs/logging. The log ─ ─ ─ ─ ─ ─ ─ ┼ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ 1 │ this 2 │ this is a after method testing. ─ ─ ─ ─ ─ ─ ─ ┴ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─Copy the code

A link to the

  • Github.com/goaop/frame…