Namespace Routes [SOLVED]

I'm having trouble understanding the namespacing for Phalcon. I have been looking at the docs and example but I am missing something, perhaps because its 4am now :) I keep getting: AccountController handler class cannot be loaded

/app/controllers/dashboard/AccountController.php

<?php
namespace Dashboard;

class AccountController extends \BaseController
{
}

/app/config/Routes.php

$this->add('/dashboard/:controller', [
            'namespace' => 'Dashboard',
            'controller' => 1,
        ]);

/public/index.php (Bootstrap)

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

$loader->registerNamespaces([
   "Dashboard" => "../app/controllers/dashboard/", // ? This doesnt do anything :(
]);

$loader->registerDirs([
    '../app/controllers/',
    '../app/controllers/dashboard/', // ? this works without namespaces :(
    '../app/controllers/admin/',
    '../app/models/',
    '../app/config/',
    '../app/event/',
    '../app/library/',
]);

$loader->register();

Can anyone tell why my namespace is not registering and behaving?



5.8k

Code looks right to me but I would add some debug statements just to be safe:

var_dump(file_exists("../app/controllers/dashboard/"));
var_dump(is_dir("../app/controllers/dashboard/"));

$loader->registerNamespaces([
   "Dashboard" => "../app/controllers/dashboard/", // ? This doesnt do anything :(
]);

// ...

$loader->register();
var_dump(class_exists("Dashboard\AccountController"));
var_dump(class_exists("\Dashboard\AccountController"));
$accounts = new Dashboard\AccountController();
$accounts = new \Dashboard\AccountController();

If the above checks out then make sure you have no other routes interfering. You can try disabling default routes by passing false to the Router contructor with:

new Router(false);
edited Mar '14

Thanks for the reply. It's not getting that namespace for some reason nor loading the goods, Im fiddling with it Ill post If I can figure it out,

bool(true) bool(true) bool(true) bool(false)
    $loader->registerNamespaces([
       "Dashboard" => "../app/controllers/dashboard/",
    ]);
    var_dump(file_exists("../app/controllers/dashboard/"));
    var_dump(file_exists("../app/controllers/dashboard/AccountController.php"));
    var_dump(is_dir("../app/controllers/dashboard/"));
    $loader->registerDirs([
        '../app/controllers/',
        '../app/controllers/dashboard/',
        '../app/controllers/admin/',
        '../app/models/',
        '../app/config/',
        '../app/event/',
        '../app/library/',
    ]);

    $loader->register();
    var_dump(class_exists("Dashboard\AccountController"));

So I re-added all my namespaces and it appears to be working. I'm kind of baffled why it wasn't before, must be coding at unholy hours. If I figure out exactly why I would share, but I did remove my register directories where the namespaces go and it all gravy:

    $loader->registerDirs([
        '../app/controllers/',
        '../app/models/',
        '../app/config/',
        '../app/event/',
        '../app/library/',
    ]);

Looks like the slash at the end, and not having the slash at the end is what is causing all my confusion in the routes, and the URL:

$this->add('/dashboard/:controller/', [
            'namespace' => 'Dashboard',
            'controller' => 1,
        ]);

Here's an adjustment for anyone that encounters the same problem to remove the trailing slash in the bootstrap .htaccess

RewriteEngine On
RewriteRule ^(.*)/$ http://%{HTTP_HOST}/$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?_url=/$1 [QSA,L]


5.8k

There is also

$di['router']->removeExtraSlashes(true);

Thanks aleemb that helps a lot!

Hi,

I have the exact same issue however I'm using nginx instead of apache. My controller is located in app/controllers/management/

namespace MK\Management\Controllers;

class DepartmentsController extends MK\Frontend\BaseController
{

}

I have other controllers located in app/controllers/frontend (these work just fine)

$router = new \Phalcon\Mvc\Router(false);
$router->setDefaultNamespace('MK\Frontend\Controllers');
$router->removeExtraSlashes(true);

$router->add(
    "/management/:controller/",
    array(
        "namespace"     => 'MK\Management\Controllers',
        "controller"    => 1
    )
);

$router->add(
    "/management/:controller/:action/",
    array(
        "namespace"     => 'MK\Management\Controllers',
        "controller"    => 1,
        "action"        => 2
    )
);

$router->add(
    "/management/:controller/:action/:params",
    array(
        "namespace"     => 'MK\Management\Controllers',
        "controller"    => 1,
        "action"        => 2,
        "params"        => 3
    )
);

$router->add(
    "/:controller",
    array(
        "namespace"     => 'MK\Frontend\Controllers',
        "controller"    => 1
    )
);

$router->add(
    "/:controller/:action",
    array(
        "namespace"     => 'MK\Frontend\Controllers',
        "controller"    => 1,
        "action"        => 2
    )
);

$router->add(
    "/:controller/:action/:params",
    array(
        "namespace"     => 'MK\Frontend\Controllers',
        "controller"    => 1,
        "action"        => 2,
        "params"        => 3
    )
);

when calling controllers in MK\Frontend\Controllers everything is fine, however when I call any controller from the MK\Management\Controllers I get the following error

MK\Frontend\Controllers\ManagementController handler class cannot be loaded

my namespace is totally being ignored!! any ideas?

Because last route added will have most priority, and your last route will match anything. :)

I know I'm late, but still someone might find it helpful.