Class with automatic view rendering


I would like to implement my own class, but not a controller, because those class can't be call from url or else.

But I wish that this class will be able to automatically, call the related view, as behave a controller.

if I call, inside the class Toto method gotZero() it will automaticaly load views/plugins/toto/gotzero.volt and render it.

Can you explain me how to implement (or example) such behavior.



Why just not extend MVC\Controller but put in folder different than controllers?

Well, I was thinking that if it extend a controller, the dispatcher will take it in. And some direct access may be executed ?

Also, I don't know how to call it from an Action or from Volt.

My need is to be able to load a various number of plugins. Each plugin will have to implement certain function. I want to be able to call those plugin from Volt and from action.

In volt for exemple i'll like to be able to do :

{% for book in books %} {% plugin::exec('pluginname', 'action', params) %} ==>will return rendered view {% endfor %}

Then in the contoller action

if($this->request->isPost()) { $model->save(); Call all the method afterSave of all active plugins. }

I'm beginner on phalcon, I do not figure out how to implement this easily


There is no option for this. Automatic rendering is happening in Phalcon/Mvc/Application which got result from dispatcher which is executing controllers. You would need to extend dispatcher and make it use your classes.

if you dont add the controller/actions in the public/private ressources in the ACL they won't be accessible for the users then you should be able to extend you class from Phalcon\Mvc\Controller without security problems.

edited Jan '17

Well I found a way using the event manager. and the fire system.

I had to load dynamically all existing plugins, and attache the events dynamically.


Hello i had the same problem , and i created an "Misc" namespace for my custom exceptions like so :

  1. add namespace to module.php


namespace Api;

use Phalcon\Loader,
    Phalcon\Db\Adapter\Pdo\Mysql as DbAdapter;

class Module
  public function registerAutoloaders()
    $loader = new Loader;

      'Api\Controllers'  => __DIR__ . '/controllers/',
      'Api\Models'       => __DIR__ . '/models/',
      'Api\Misc'         => __DIR__ . '/misc/'      // <<<<=== here


2 - create the folder "misc"

3 - in the folder create your class like so:


namespace Api\Misc; // <== namespace we just created

use \Phalcon\Exception;

class ApiExceptions extends \Phalcon\Exception // <= class name so we can access it later in our controllers
  public static function InvalidCsrfToken()
    throw new Exception("Invalid CSRF Token", 6);

4 - start using it !

use Api\Misc\ApiExceptions  as ApiException; // <== class name that we created