phalcon: Profiling分析 profilter / Plugin结合,dispatcher调度控制器 监听sql执行日志
个人觉得profilter 跟 logger 功能差不多,logger的功能在于写入,profilter功能在于sql后及时显示分析。都是对sql执行的的分析:一个是写入log文件,一个是直接在页面展示。
下面看例子,
public/index.php:
$di->set('profiler', function(){
return new \Phalcon\Db\Profiler();
}, true);
$di['db'] = function() use($di){
//profile
$eventManager = new \Phalcon\Events\Manager();
$profiler = new ProfilerEManger();
$eventManager->attach('db', $profiler);
$db = new DbAdapter(array(
"host" => "localhost",
"username" => "root",
"password" => "",
"dbname" => "demo",
"charset" => "utf8"
));
$db->setEventsManager($eventManager);
return $db;
};
app\plugins\ProfilerEManger.php
use \Phalcon\Mvc\User\Plugin;
class ProfilerEManger extends Plugin {
public function beforeQuery()
{
// var_dump($this->db->getSqlStatement());exit;
$this->profiler->startProfile($this->db->getSqlStatement());
}
public function afterQuery()
{
$this->profiler->stopProfile();
}
}
ProfilerController.php
class ProfilerController extends \Phalcon\Mvc\Controller {
public function indexAction()
{
$users = array();
$use = \Users::findFirst("id = 1");
if($use)
{
$users = $use->toArray();
}
var_dump($users);
$profile = $this->profiler->getLastProfile();
var_dump($profile);
}
}
浙公网安备 33010602011771号