PHP Phalcon:没有视图


PHP Phalcon: no views?

我正在评估与API一起使用的框架,并且我正在认真研究PHP Phalcon。它看起来很有前途——"精简"(加载你需要的东西),但有很多选项。

我想知道……是否有可能使用视图(模板,更确切地说)?我是否必须设置一个视图,或者我可以只输出。json?

谢谢!

在Phalcon中有一种方法可以在操作中禁用视图并避免不必要的处理:

public function indexAction() {
    $this->view->disable();
    $this->response->setContentType('application/json');
    echo json_encode($your_data);
}

根据你想做什么,你可以禁用视图,其他人建议和返回json编码的数据,或者你可以使用内置的响应对象,如下所示:

  $this->view->setRenderLevel(View::LEVEL_NO_RENDER);
  $this->response->setContentType('application/json', 'UTF-8');
  $this->response->setJsonContent($data); //where data is an array containing what you want
   return $this->response;

文档中还有一个教程,介绍如何构建一个简单的REST api

http://docs.phalconphp.com/en/latest/reference/tutorial-rest.html

如果你不使用任何视图,你可以在一开始就禁用视图。

$app = new 'Phalcon'Mvc'Application();
$app->useImplicitView(false);

即使你这样做了,我认为你仍然需要设置视图DI来让框架工作。

如果你想输出json也有一个方法:

$this->response->setJsonContent($dataArray);
$this->response->send();

是的,你可以做到,我用PHP Phalcon。要忽略视图,在你的控制器中你的动作应该像

public function indexAction() {
   $var = array or other data 
   die(json_encode($var));
}

die();在控制器将不呈现任何父布局!:)