We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Disabling views for my Android app using Phalcon RestFul APIs

I am trying to develop a MVC application totally based on REST APIs. Case 1: If I want to use web browser, then I want to keep views enabled. Case 2: If I want to use app, then I want the REST API to return JSON output and disable the views.

Moreover, is there any generic way to handle both the cases simultaneously?

Any help would be appreciated !!

I am not sure did i understand you... but u can detect which case it is (based on request type) and return the needed content type (json or xml or html...). Other way is to add extension - for example .xml, .json (in router detection).

Thanks Boris for replying but I am pretty new to phalcon, so do not have much idea about it. And I didn't find any illustration showing this type of case in phalcon official documentation. Can you please elaborate on this? or can you please give an example/pseudocode if possible?

Sure :)

But first i recommend u to see the examples (toturials)... and pick the right module/micro part (see https://github.com/phalcon/mvc ).

So, lets explain case when we use request to get which type we must return. It is pretty simple. U need to use request... by example: (this is action in controller)

$this->request->isAjax() - will check if request is ajax and then u can return json (for example). You can use functions like isDelete, isPut, isPost, isOptions. For more details visit: https://docs.phalcon.io/en/latest/reference/request.html ..thats all :)

The second case.. or what kind of data will return based by router its simple too - maybe in router params is our need - check if params is .json and return it.

https://docs.phalcon.io/en/latest/reference/routing.html - routing documentation.

And the last- how to return HTML or JSON (example)

    function someAction()
    {
      // logick

      // Something we need
      $content = new stdClass;
      $content->some = true;

      // If ajax so return JSON
      if ($this->request->isAjax()) {
          $this->view->disable();
          return $this->response->setJsonContent($content);
      } 

      $this->view->content = $content;
    }

But there are so many methodologies. Check these resources : https://phalconist.com/search?q=rest

And last... sorry for my english :/ Good luck :)