Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

How to disable views in RESTful API app

Hi,

I'm building huge API RESTful application with Phalcon, my application doesn't have any views at all. Every request is replayed in JSON format. I don't need views at all, but when I´m deleting the view registration from service.php I got error

'Service 'view' was not found in the dependency injection container

which is OK because in the index.php file :

echo $application->handle()->getContent();

I saw that I can use Phalcon\Mvc\Micro(); but this is not good enough for me because my application in not Micro :)

Also I know that I can disable view e.g :

$view->disable();

but I don't want to use it all

My question is how/is it possible to use \Phalcon\Mvc\Application($di) without to use views?

edited Jul '14

You could create a central Controller as base inherited by all your controllers.

In there you could disable the views globally.



83.4k
Accepted
answer

There's no way to disable the view component since Phalcon\Mvc\Application requires it to pass the control flow of the request once it finishes executing.

You can just register it without setting a views directory:

$di->set('view', function() {
    return \Phalcon\Mvc\View();
}, true);

Or you can create your own Phalcon\Mvc\Application as explained here: http://docs.phalconphp.com/en/latest/reference/applications.html#manual-bootstrapping

this will disable the views for any application:

$api = new Application(); $api->useImplicitView(false)->main();

http://api.phalconphp.com/en/1.3.3/Phalcon/Mvc/Application#useImplicitView-details