Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Phalcon: no views?

I'm evaluating frameworks for use with an API, and I'm taking a serious look at PHP Phalcon. It's looking promising - "lean" (load what you need), but with a lot of options.

I'm wondering... is it possible to not use views (templates, rather) with it? Do I have to set up a view, or can I just output .json?

Thanks!

like image 577
Mr Mikkél Avatar asked Jul 18 '14 20:07

Mr Mikkél


2 Answers

There is a way in Phalcon to disable view in the action and avoid unnecessary processing:

public function indexAction() {

    $this->view->disable();

    $this->response->setContentType('application/json');
    echo json_encode($your_data);
}
like image 183
Phantom Avatar answered Sep 21 '22 00:09

Phantom


Depending on what you want to do you can disable the view as others have suggested and echo json encoded data, or you could use the built in response object as below:

  $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;

There is also a tutorial in the docs that goes over building a simple REST api

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

like image 28
TommyBs Avatar answered Sep 19 '22 00:09

TommyBs