Phalcon 常用操作

接受请求数据

#控制器中接收数据
#在浏览器中访问 http://localhost/index/test1?a=1&b=2 
$this->request->get() 方法能同时获取 GET 和 POST 请求的数据;
$this->request->getQuery() 只能获取 GET 方式的请求数据;
$this->request->getPost() 只能获取 POST 方式的请求数据。

#在Phalcon的路由匹配规则中,我们可以通过 $dispatcher来接收数据:
#但是需要注意的是这个需要在路由配置中进行设置
    public function test3Action(){
        $a = $this->dispatcher->getParam('a');
        $b = $this->dispatcher->getParam('b');
        var_dump($a);
        var_dump($b);
    }
#路由规则如下( app/config/routes.php):
    '/index/test3/(\d+)/(\d+)' => array(
        'module' => 'frontend',
        'controller'=>'index',
        'action'=>'test3',
        'a' => 1,
        'b' => 2,
    ),

响应数据格式

#返回json格式数据
public function test6Action(){
        return $this->response->setJsonContent(array(
            'code' => 1,
            'message' => 'success',
        ));
    }

页面跳转

#redirect(),仔细观察会发现浏览器中的URL地址已经发生了变化。
    public function test4Action(){
        return $this->response->redirect('https://www.marser.cn');
    }


#forward(),此种方式的页面跳转不会改变URL地址,只是将请求转发到另一个控制器的action。
    public function test5Action(){
        return $this->dispatcher->forward(array(
            'controller' => 'test',
            'action' => 'index',
        ));
    }

调用DI中注册的服务

#DI中注册的所有服务,在控制器中都可以直接调用:
    public function test7Action(){
        var_dump($this->session);
        var_dump($this->cookies);
        var_dump($this->request);
        var_dump($this->response);
        var_dump($this->db);
        var_dump($this->logger);
        //...
    }
#我们可以在这里发散一下,在DI中注册我们的全局配置对象:

$di -> setShared('config', function() use($config){
    return $config;
});
#在控制器中直接调用( $this->config)即可。

跟踪sql

$di->setShared('db', function () use ($config, $di) {
    $connection = new Phalcon\Db\Adapter\Pdo\Mysql($config->toArray()["db"]);
    $profiler = $di['profiler'];
    if( $profiler ){
        $eventsManager = new EventsManager();
        $eventsManager->attach('db', function($event, $connection) use ($profiler) {
            if ($event->getType() == 'beforeQuery') {
                //Start a profile with the active connection
                $profiler->startProfile($connection->getSQLStatement());
            }
            if ($event->getType() == 'afterQuery') {
                //Stop the active profile
                $profiler->stopProfile();
            }
        });
        $connection->setEventsManager($eventsManager);
    }
    return $connection;
});

$di->setShared("profiler", function () use ($config) {
    if (!$config->global->debug){
        return false;
    }
    $profiler = new \Phalcon\Db\Profiler();
    return $profiler;
});

function getLastSQLStatement()
{
    $profile_init = $di['profiler'];
    if(!$profile_init){
        return "please set your debug mode true";
    }
    $profile = $profile_init->getLastProfile();
    $db =  $di['db'];
    return $arr = [
        'SQL Statement' => $profile->getSQLStatement(),
        'Params' => $db->getSQLVariables(),
        'Start Time' => $profile->getInitialTime(),
        'Final Time' => $profile->getInitialTime(),
        'Total Elapsed Time:' => $profile->getTotalElapsedSeconds(),
    ];
}