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

dispatcher controllerName actionName moduleName is empty

Hello,

I'm trying to show some debug info after script ends. I want to show module, controller and action names which was rendered. It is important, because I do some forwards, and I need to know. I tried to get module, controller and action names, but they are empty.

What is the correct way to get last called module/controller/action names?

edited Nov '14

You should create a base controller, likes this:


<?php

use Phalcon\Mvc\Dispatcher;

class ControllerBase extends \Phalcon\Mvc\Controller
{
    public function beforeExecuteRoute(Dispatcher $dispatcher)
    {
        $controllerName = $dispatcher->getControllerName();
        $actionName = $dispatcher->getActionName();
    }

    public function afterExecuteRoute(Dispatcher $dispatcher) {
        $controllerName = $dispatcher->getControllerName();
        $actionName = $dispatcher->getActionName();
    }

}

then all controllers will extend that controllerbase

Actually the problem was:

$di->get('dispatcher');

created new instance. So I changed it to:

$di->getShared('dispatcher');

and got full dispatcher.