2016-07-01 54 views
3

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

我将非常感激这些信息。谢谢大家。

回答

4

我通常使用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; 
} 

我还发现在这个文档Performance Profiling,但我还没有尝试过任何解决方案。

+0

非常感谢您的回答!我会试试看。 –

+0

你是否得到它的工作? –

+0

是的,我做到了。再次感谢您的回复。 –