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

ActionName from dispatcher starting uppercase

Not really a problem but I would like to know if getting actionName from dispatcher (in beforeExecuteRoutein my case) camelcase but starting with uppercase is normal? I took vokuro as exemple for ACL, in this app it uses "normal" camelCase to defines some action but got a problem whit my camelCase definition of action.

IMO dispatcher getActionName should returns the real action name minus "Action", So in my case, with this action deleteSeriesAction it should return deleteSeries and not DeleteSeries (as it returns actually)

First time I hear about this :) Maybe there is extended dispatcher method somewhere in the code? Or even a bug.. what phalcon version are you using?

    // Blog: Detail
    public function blogViewAction()
    {
        die($this->dispatcher->getActionName()); // Correctly returns: blogView


8.2k

I update to v3.3.0 just before doing the ACL

Versions:
  Phalcon DevTools Version: 3.2.5
  Phalcon Version: 3.3.0
  AdminLTE Version: 2.3.6

Here is where I use it (in a ControllerBase)

public function beforeExecuteRoute(Dispatcher $dispatcher)
    {
        $controllerName = $dispatcher->getControllerClass();

        // Only check permissions on private controllers
        if ($this->acl->isPrivate($controllerName)) {

            // Get the current identity
            $identity = $this->auth->getIdentity();

            // If there is no identity available the user is redirected to index/index
            if (!is_array($identity)) {

                $this->flashSession->error('You don\'t have access to this module: private');
                $dispatcher->setNamespaceName('App\Controllers');
                $dispatcher->forward([
                    'controller' => 'index',
                    'action' => 'index'
                ]);
                return false;
            }

            // Check if the user have permission to the current option
            $actionName = $dispatcher->getActionName();
            //hack to have "real" camelCase, starting by lowercase char
            $actionName = lcfirst($actionName);

            if (!$this->acl->isAllowed($identity['profile'], $controllerName, $actionName)) {

                $this->flashSession->notice('You don\'t have access to this module: ' . $controllerName . ':' . $actionName);

                if ($this->acl->isAllowed($identity['profile'], $controllerName, 'index')) {

                    $dispatcher->setNamespaceName($dispatcher->getNamespaceName());
                    $dispatcher->forward([
                        'controller' => $dispatcher->getControllerName(),
                        'action' => 'index'
                    ]);
                } else {
                    $response = $this->response;
                    $this->auth->remove();
                    //force the namespace
                    $dispatcher->setNamespaceName('App\Controllers');
                    $dispatcher->forward([
                        'controller' => 'index',
                        'action' => 'index'
                    ]);
                }
                return false;
            }
        }
    }

From vokuro ControllerBase, I changed the check on controllerName only to controllerClass, because I have multiple times the same controller name, but not in the same namespace/folder and the lcfirst to get real cameCase on the actionName (and the redirection too)

The actions definition in a privateResources config file are in "normal" camelCase, same for the permissions in db.

Just thought about this, but I use this routing rule to match my private resources

$router->add(
    '/admin/:controller/([a-zA-Z0-9_-]*)/:params',
    [
        'namespace'  => 'App\Controllers\Admin',
        'controller' => 1,
        'action'     => 2,
        'params'     => 3,
    ]
)->convert('action', function($action) {
    return Phalcon\Text::camelize($action);
});

Don't think it's related to the camelize function, though.



145.0k
Accepted
answer

No, this is true, Phalcon\Text::camelize just returns PascalCase really or UpperCamelCase.

So i guess we need real camelize method and pasalize. Heh.

Though we could argue, that camelize works correctly and just returns CamelCase and if you want camelCase you need to lcfirst.



8.2k

Well i had to add the camelize function to the route to accept the dashed url and it redirects well to my camelized action. but I don't think it's a good thing that camelize returns UpperCamelCase in the Dispatcher.

Btw thank for the answers and clarifications

@etshy

You can always add your own Text class and extends phalcon existing one and override camelize method.