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

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.



98.9k
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: https://docs.phalcon.io/en/latest/reference/applications.html#manual-bootstrapping

this will disable the views for any application:

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

https://api.phalcon.io/en/1.3.3/Phalcon/Mvc/Application#useImplicitView-details