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 short syntax and multi-word controller names

The short syntax seems to be incompatible with controller classes containing multiple words.

<?php

class ProductCategoryController {
  function indexAction() { ... }
}

$router->add("/products/categories", "ProductCategory::index");
print $router->getControllerName();

getControllerName() returns an all lowercase controller name and the dispatcher looks for a class called ProductcategoryController. Instead of simply lowercasing wouldn't it make more sense to use snake-case. Then transformation can be reversed and the dispatcher can resolve the controller name properly.



98.9k

Good point and I agree, can you please open a NFR for this on Github? https://github.com/phalcon/cphalcon



98.9k

Thanks Tim

I know this post is old, but this should also be added in docs since is quite important. It took me 2 hours to find how phalcon is handling multi word controllers. Thanks

Hi @Tim Robertson, I am facing the same issue now. If you have an solution could you please suggest me on this? Thank you

@Karthik261193 This could be a file naming issue. On Windows, file names are matched in a case insensitive manner. On other systems, it is case sensitive.
Make sure your controller can be found and loaded when using lowercase. If you change the case, git won't detect the change, the suggestion for that is rename it entirely so git can detect the change, then rename back to fix the case. Also note you need the word "Controller" on the end. Even if the Phalcon error doesn't specify "Controller" on the end, it is required.

Tim's issue was fixed back in Phalcon 1.2.0, four and a half years ago, as per his link to issue 693:
https://github.com/phalcon/cphalcon/issues/693

While snake_case, to the best of my understanding, is still used internally by Phalcon, it should be locating your controller with the same casing as you told the router. Just make sure you define the controller with "Controller" on the end, but don't tell the router it has "Controller" on the end.

In other words:

// e.g. /services/router.php
$router->add("/products/categories", "ProductCategory::index"); // no Controller suffix, no Action suffix

// e.g. /Controllers/ProductCategoryController.php
class ProductCategoryController extends \Phalcon\Mvc\Controller
{
    public function indexAction()
    {
        // do stuff
    }
}