Yii 2 有 AOP 吗?

Is there AOP for Yii 2?

我想知道每个动作执行了多少时间。最简单/正确的方法是使用 AOP。

我想要这样的东西:

/**
 * @FLOW3\Before("method(.*->action.*())")
 */
 public function markFirstTimeTag() {
// Do something here.
 }

 ...

 /**
 * @FLOW3\After("method(.*->action.*())")
 */
 public function markSecondTimeTag() {
// Do something here.
 }

我读到了 FLOW3 和我喜欢的这个框架。但这本身就是一个全栈框架。

Yii 2有AOP模式的实现吗?

我通常使用 Logging 来分析我的代码。

Yii::trace('starting some event');
foreach(..)
{
    ...
}
Yii::trace('some event done');

可以在调试栏的日志部分找到此跟踪。

这可以与 beforeAction() and afterAction() 结合使用(未测试)

public function beforeAction($action)
{

    if (!parent::beforeAction($action)) {
        return false;
    }

    Yii::trace($action->id.' started');

    return true; // or false to not run the action
}

public function afterAction($action, $result)
{
    $result = parent::afterAction($action, $result);
    Yii::trace($action->id.' ended');
    return $result;
}

我也在文档中找到了 Performance Profiling,但我没有尝试过任何解决方案。