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

Router issue [xxxController handler class cannot be loaded]

Hi, I am working on a Phalcon project. I can execute all my controller and action by giving the/controllername/actionname in url. The routes which I have added using the Router class are not working.

As per below router, https://localhost/myproject/api/list must load the controller 'Clients' and the action 'index'.

$router = new \Phalcon\Mvc\Router();
$router->add(
    "/api/list",
    array(
        "controller" => "Clients",
        "action"     => "index",
    )
);
$router->handle();

I added the following lines below the $router->handle();

echo '<br />';
echo 'Controller: '. $router->getControllerName();
echo '<br />';
echo 'action: ' . $router->getActionName();
echo '<br />';

It gives me the output

Controller: Clients
action: index

But at the end of the page I am getting,,

ApiController handler class cannot be loaded

The framework still trying to load the controller Api with action name list directly from URL which doesn't exist.



7.9k

Are you using namespace in your controlller class?

If yes you have to registed controller with its full namespace

I am using namespace for others except for the Controllers. Normally the controllers works except which the route which I have defined in the rotuer.

Are you using namespace in your controlller class?

If yes you have to registed controller with its full namespace

If I want to use namespace, how would I register it with namespace? There is only a variable to set controllersDir which is a path not the namespace.

edited Apr '15

Hello,

All you have to do is to register the namespace root and everything under it will be loaded automatically.

In your controllers files just add on top

namespace Controllers;

and in bootstrap / loader file register the namespace

// Creates the autoloader
$loader = new \Phalcon\Loader();

//Register some namespaces
$loader->registerNamespaces(
    array(
       "Example"         => "path/to/example/",
       "Controllers"    => "path/to/controllers/"
    )
);

// register autoloader
$loader->register();

https://docs.phalcon.io/en/latest/reference/loader.html

https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md

I will try it one I move my controller to namespace based. First I am trying to solve the first issue. It ws not solved still.

if you look at the question, my router identifies the correct controller and action but the dispacher not.

echo $router->getControllerName(); // outputs "Clients" while phalcon tries to load the `ApiController` class

How can I solve this issue?



1.9k
Accepted
answer

I was able to solve this issue by following a question posted in stackoverflow.com How router/roites could be injected in application in phalcon framework? That worked for me.

The working solution is below.

I inserted the following piece of code in my index.php right after loading service..

    /**
     * Read services
     */
    include __DIR__ . "/../config/services.php";

    $di->set('router', function() {
        $router = new \Phalcon\Mvc\Router();
        $router->add(
            "/api/list",
            array(
                "controller" => "Clients",
                "action"     => "index",
            )
        );
        return $router;
    });

The above looks fine, but the implementation which I took from Phalcon Documentation Routing|Phalcon did not help me. I think it is much more generic and did not suit to my environment.

I see.. what you didn't do is to register it in dependency injector it can be done the way you found, or just add

$di->set('router', $router)

to your existing code

registering first the service and then adding it in DI can be useful in some cases when you need to use its values later on in the same function.