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

methods that will always be executed

I need to insert some methods that will always be executed on the system, eg check if the system is active or maintenance (getting some data of database), check if the user was locked or not, among others. What would be the best place to put these methods?



3.1k
Accepted
answer

Put your logic in a class, and instantiate an instance of that class in your Dependency Injector (in app/services.php or wheverer). Then create a "BaseController" that acts upon your class in a 'beforeExecuteRoute' method, and extend your other controllers from you 'BaseController'. You should be all set.

E.g.:

class BaseController extends Phalcon\Mvc\Controller { public function beforeExecuteRoute(Dispatcher $dispatcher) { if($this->myClass->maintenanceModeActive()) { $dispatcher->forward(array( 'controller' => 'maintenance', 'action' => 'index', 'params' => array('forwarded' => 'true') )); return false; } } }