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

Route controller and action uncamelize in router

Hi,

I think for best integrity and clear behaviur in routing, the actionName should be uncamelize too as controllerName does already.

See this section of source

    ...
                    // Always pass the controller to lowercase
                    let routePaths["controller"] = uncamelize(realClassName);
                }

      // Process action name
      if actionName !== null {
            let routePaths["action"] = actionName; // <-- should be uncamelize(actionName)
      }
...

I'm not sure this breaks somthing else or not!

Any idea?

Thanks.

Thanks Andress,

I already do that before in my route setup by using convert() method.

/* Route to support dashes in action and controller name */
$router->add('/:controller/([a-zA-Z\-]+)/:params', array(
    'controller' => 1,
    'action' => 2,
    'params' => 3
))->convert('action', function($action) {
    return lcfirst(Phalcon\Text::camelize($action));
});

But I faced this issue when I was trying to route to a CamelCase controller as follow:

$router->add('/manage-user/edit/:action/:int/:params', array(
    'controller' => 'manage-users',
    'action'    => 1,
    'id'        => 2,
    'params'    => 3
))->setName('manage-user');

I tried "manageUsers" as controller name with no lock, so while browsing the source I see the defualt uncamelize functionality in controller's class name. so I wondered that why controller name uncamelized but action name did not!

Thank you for your reply. and sorry for my english anyway.

You can change that behaviour this way: https://docs.phalcon.io/en/latest/reference/dispatching.html#camelize-action-names

By doing that in the dispatcher as it's explained in the example I attached you won't have problems in regards of the source of the controller/action name. This is a legacy behaviour kept from version to version to not break existing applications.

You'r right. But can you please explain that why controller's name already uncamelized in source but action didn't?

Thanks.

By doing that in the dispatcher as it's explained in the example I attached you won't have problems in regards of the source of the controller/action name. This is a legacy behaviour kept from version to version to not break existing applications.



34.6k
Accepted
answer

I explained it:

This is a legacy behaviour kept from version to version to not break existing applications.

@andresgutierrez in your dispatcher centric solution (also in Doc), we should use lcfirst() to prevent action call issue:

$dispatcher->setActionName(lcfirst(Text::camelize($dispatcher->getActionName())));

Thanks.

I explained it:

This is a legacy behaviour kept from version to version to not break existing applications.