Yii 2有AOP吗?

gywdnpxw  于 2022-11-09  发布在  其他
关注(0)|答案(1)|浏览(172)

我想知道每个动作执行的时间是多少。最简单/正确的方法是使用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模式的实现吗?

zbdgwd5y

zbdgwd5y1#

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

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

此跟踪可在调试栏的“日志”部分找到。
这可以与beforeAction()和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;
}

我还在文档中找到了性能分析,但我没有尝试过任何解决方案。

相关问题