I tried naming my controller TestMeNowController.php and it wont load it trowing this error > 'App\Test\TestmenowController handler class cannot be loaded' For some reason phalcon changing camelized controller name to lowercase. Camelizing my controllers is very important to organize my current project.
app/config/bootstrap.php
$di->set('dispatcher', function() {
$dispatcher = new Dispatcher();
$dispatcher->setDefaultNamespace('App\Controllers');
return $dispatcher;
});
$loader->registerNamespaces([
'App\Controllers' => '../app/controllers/',
'App\Controllers\Test' => '../app/controllers/test/',
'App\Libraries' => '../app/libraries/'
])->register();
$di->set('router', function() {
$route = new Router(false);
require '../app/config/routes.php';
return $route;
});
app/config/routes.php
$route->notFound([
'controller' => 'base',
'action' => 'route404'
]);
$route->add('/', [
'namespace' => 'App\Controllers\Test',
'controller' => 'TestMeNow',
'action' => 'index'
]);
app/controllers/BaseController.php
namespace App\Controllers;
class BaseController extends Controller {
function route404 {
exit('404');
}
}
app/controllers/test/TestMeNowController.php
namespace App\Controllers\Test;
use App\Controllers\BaseController;
class TestMeNowController extends BaseController {
function indexAction() {
echo 'TestMeNowController';
}
}
The only workaround I have found is changing my routes name like this.
$route->add('/', [
'namespace' => 'App\Controllers\Test',
'controller' => 'test_me_now',
'action' => 'index'
]);
But it would be nice if we could just use "TestMeNow" instead. Even if we have to use some sort of routes option like $route->setCamelizeControllers(true);
So my feature request would be something like this;
$di->set('router', function() {
$route = new Router(false);
$route->setCamelizeControllers(true);
require '../app/config/routes.php';
return $route;
});
Yea +1 for this. Hate calling my controllers with only one big letter.
@tuxy https://github.com/phalcon/cphalcon/blob/master/phalcon/dispatcher.zep#L630
$di->set("dispatcher", function () {
$eventsManager = new Phalcon\Events\Manager();
$eventsManager->attach("dispatch:beforeDispatchLoop", function ($event, $dispatcher) {
$dispatcher->setControllerName(Phalcon\Text::uncamelize($dispatcher->getControllerName()));
});
$dispatcher = new Phalcon\Mvc\Dispatcher();
$dispatcher->setEventsManager($eventsManager);
return $dispatcher;
}, true);
@freekzy This is actually very ugly way of doing this ^_^ But thanks anyway.
@andresgutierrez I know its not a bug, this is why I requested this feature.
@tuxy Anyway, usage of capital chars in URI is a bad practice.
@freekzy Not in URI in Class names only.
This is MVC convention. AccountController maps /account, AccountEditController maps to /account_edit, and MyBestEverAccountEditController maps to /my_best_ever_account_edit. This isn't going to change.
Most helpful comment
@tuxy https://github.com/phalcon/cphalcon/blob/master/phalcon/dispatcher.zep#L630