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

Help a Beginner Routing

Hello. I have the next folder structure:

app
 |-config
    |-RoutesGroup.php
 |-controllers
    |-CustomFolder
       |-TestBaseController.php
       |-TestController.php
    |-MyFolder
       |-MyController.php
public
 |-index.php

I have in my index.php:

<?php

//Autoloader
    $o_loader = new \Phalcon\Loader();
    $o_loader->registerDirs([
        '../app/controllers/'
        , '../app/models/'
        , '../app/config/'
    ]);
    $o_loader->registerNamespaces([
        'App\Controllers\customFolder' => '../app/controllers/customFolder/',
        'App\Controllers\myFolder' => '../app/controllers/myFolder/',
    ]);
    $o_loader->register();

//Routing
    $o_di->set('router', function () {
        $o_router = new \Phalcon\Mvc\Router(false);
        $o_router->mount(new RoutesGroup(array('namespace' => 'App\Controllers\CustomFolder')));
        return $o_router;
    });

An in my RoutesGroup.php

<?php

use Phalcon\Mvc\Router\Group;

class RoutesGroup extends Group {
    public function initialize($config) {
        if (array_key_exists('perfix', $config)) {
            $this->setPrefix('/' . $config['perfix']);
        }
        $this->add('/:controller/:action/', array(
            'namespace' => $config['namespace'],
            'controller' => 1,
            'action' => 2
        ));
    }
}

My local domain is https://gms.phalcon.

When I try to execute https://gms.phalcon/test/index, the server resonse is:

IndexController handler class cannot be loaded

What I'm doing wrong?



1.9k

It seems that missing namespace. $config[namespace]?

Hi ntesic.

Well that value is passing at:

$o_router->mount(new RoutesGroup(array('namespace' => 'App\Controllers\CustomFolder')));

It seems that missing namespace. $config[namespace]?



1.9k

So var_dump($config[namespace]) give you App\Controllers\CustomFolder?

Yes. I don't know what's wrong.

So var_dump($config[namespace]) give you App\Controllers\CustomFolder?



3.6k
edited Nov '16

Depending on your server OS, it looks like you could be running into a casing issue. For example, you have lower cased customFolder and myFolder when registering the namespaces below:

    $o_loader->registerNamespaces([
        'App\Controllers\customFolder' => '../app/controllers/customFolder/',
        'App\Controllers\myFolder' => '../app/controllers/myFolder/',
    ]);

But reference those namespaces differently when setting up the routing:

$o_router->mount(new RoutesGroup(array('namespace' => 'App\Controllers\CustomFolder')));

Would having the following make a difference? Also note the directory names, if those are lower camel cased or not. In the directory structure you have outlined, it should look like the following:

    $o_loader->registerNamespaces([
        'App\Controllers\CustomFolder' => '../app/controllers/CustomFolder/',
        'App\Controllers\MyFolder' => '../app/controllers/MyFolder/',
    ]);


6.0k

Maybe a typo in $config['perfix'] ?