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

Events

Hello everyone. There is a basic controller BackendController. Inherited by other controllers. Please help me to organize for their support of the event, ie, to be able to expand with ItemController CategoryController and vice versa, for example.

<?php
//BackendController
namespace Core\Controllers;

class BackendController extends \Phalcon\Mvc\Controller
{}

//ItemController
class ItemController extends \Core\Controllers\BackendController
{
    public function indexAction()
    {

    }
}

//CategoryController
class CategoryController extends \Core\Controllers\BackendController
{
    public function indexAction()
    {

    }
}

//PageController
class PageController extends \Core\Controllers\BackendController
{
    public function indexAction()
    {

    }
}
//etc

Please help me to organize dynamically opportunity to expand one class by another, a rough example:

<?php
//ItemController
class ItemController extends \Core\Controllers\BackendController
{
    public function indexAction()
    {
        echo 'ItemController';
    }
}

//CategoryController
class CategoryController extends \Core\Controllers\BackendController
{
    public function indexAction()
    {
        $extraData = 'extends from CategoryController';
        $eventsManager->fire("item:beforeIndexAction", $this, $extraData);
    }
}

Accordingly, when the method is called in the controller indexAction ItemController, should receive:

<?php
    ItemController extends from CategoryController

Ideally, I can extend action of any controller in another controller action of any inherited from BackendController

Life example. When editing the item(ItemController editAction) I want to load a list of categories(CategoryController getCategoriesAction), but ItemController should not depend on CategoryController, so if i delete CategoryController, ItemController should continue to work, but list of categories(CategoryController getCategoriesAction) would no longer be available on it.



7.9k

Hi,

Could you pleas translate your post in english, so we here can help you

Hi,

Could you pleas translate your post in english, so we here can help you

Hi, translated it, but my english not good enough :)



7.9k

Hi,

extending or call another controller in controller is bad practice. Controller is just for manage routing, your business process should be placed in model.

If you want your business process reusable you use additional layer e.g service layer.

if you want learn more about service layer : https://www.youtube.com/watch?v=ajhqScWECMo

Hi,

extending or call another controller in controller is bad practice. Controller is just for manage routing, your business process should be placed in model.

If you want your business process reusable you use additional layer e.g service layer.

if you want learn more about service layer : https://www.youtube.com/watch?v=ajhqScWECMo

I only want create plugins system like Wordpress CMS or similar...

For example, i have two controllers - ItemController and CategoryController

ItemController

<?php

class ItemController
{
    public function editAction($extraData)
    {
        var_dump($extraData);
        //array('category 1', 'category 2', 'category 3')
    }
}

CategoryController

<?php

class CategoryController
{
    public function listAction()
    {
        $categories = array('category 1', 'category 2', 'category 3');
        $eventsManager->fire("ItemController:editAction", $extraData); //like this
    }
}

When i call ItemController::editAction i want get CategoryController::listAction, thats all. I think need use https://docs.phalcon.io/en/latest/reference/events.html , but how use it for this i not understand :(



7.9k

I understand,

But it is more manageable if your plugin have their own interface instead using controller as plugin.

for example this logger class will listen to any event

 <?php

namespace Avent\Events;

use Avent\Core\Event\Abstraction\BeforeAnythingAbstract;
use FastRoute\Dispatcher;
use League\Event\EventInterface;

class LoggerHook extends BeforeAnythingAbstract
{
    public function handle(EventInterface $event)
    {
        $this->logger->addDebug("{$event->getName()} triggered " . date('Y-m-d H:i:s'));
    }
}

// EOF

and for example, somewhere in my controller

 $this->event_emitter->emit("after.user.registration", $command);

I trigger listerner that listening on event after.user.registration since my listener listen on any event it wil called first

I understand,

But it is more manageable if your plugin have their own interface instead using controller as plugin.

for example this logger class will listen to any event

<?php

namespace Avent\Events;

use Avent\Core\Event\Abstraction\BeforeAnythingAbstract;
use FastRoute\Dispatcher;
use League\Event\EventInterface;

class LoggerHook extends BeforeAnythingAbstract
{
   public function handle(EventInterface $event)
   {
       $this->logger->addDebug("{$event->getName()} triggered " . date('Y-m-d H:i:s'));
   }
}

// EOF

and for example, somewhere in my controller

$this->event_emitter->emit("after.user.registration", $command);

I trigger listerner that listening on event after.user.registration since my listener listen on any event it wil called first

Thanks, i want this


class TestController
{
 public function indexAction()
 {
     //
 }
}

//And then on any controller like this

$this->fire->testcontroller->beforeIndexAction('beforeIndexAction testcontroller'); //or other syntax, does not matter

or 

$this->fire->testcontroller->afterIndexAction('afterIndexAction testcontroller'); //or other syntax, does not matter

but i dont understand how do like this on Phalcon :(