Class with automatic view rendering

Hi,

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.

Regards



1.7k

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

regards

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.

Regards

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

  1. add namespace to module.php

<?php

namespace Api;

use Phalcon\Loader,
    Phalcon\Mvc\View,
    Phalcon\Config\Adapter\Ini,
    Phalcon\Mvc\ModuleDefinitionInterface,
    Phalcon\Db\Adapter\Pdo\Mysql as DbAdapter;

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

    $loader->registerNamespaces([
      'Api\Controllers'  => __DIR__ . '/controllers/',
      'Api\Models'       => __DIR__ . '/models/',
      'Api\Misc'         => __DIR__ . '/misc/'      // <<<<=== here
    ]);

    $loader->register();
  }

2 - create the folder "misc"

3 - in the folder create your class like so:


<?php

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