phalcon控制器方法返回json

2,585次阅读

共计 897 个字符,预计需要花费 3 分钟才能阅读完成。

Base 控制器实现 afterExecuteRoute 方法:

/**
     * @param Dispatcher $dispatcher
     */
    public function afterExecuteRoute(Dispatcher $dispatcher)
    {$return = $dispatcher->getReturnedValue();
        if ($this->request->isAjax() === true || is_array($return)) {$this->view->disableLevel(array(View::LEVEL_ACTION_VIEW => true,
                View::LEVEL_LAYOUT => true,
                View::LEVEL_MAIN_LAYOUT => true,
                View::LEVEL_AFTER_TEMPLATE => true,
                View::LEVEL_BEFORE_TEMPLATE => true
            ));
            $this->response->setContentType('application/json', 'UTF-8');
            $return['code'] = isset($return['code'])  ? $return['code'] : 1;
            $return['message'] = isset($return['message']) ? $return['message'] : '';
            $this->response->setJsonContent($return);
        }
        return $this->response->send();}

入口文件:

$di = require dirname(__DIR__) . '/bootstrap.php';
$application = new Phalcon\Mvc\Application($di);
try {/*
     * @var $response Response
     */
    $response = $application->handle();
    echo $response->getContent();} catch (\Exception $e) {echo 'Exception:', $e->getMessage();}

正文完
 
Blood.Cold
版权声明:本站原创文章,由 Blood.Cold 2019-06-03发表,共计897字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。