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

Camelize action name

Hello!

I've been attempting to camlize my action names according to this instruction: https://github.com/phalcon/docs/blob/master/en/reference/dispatching.rst#camelize-action-names

However, whenever I enter a hyphen instead of a camlized name, "$dispatcher->getActionName()" is always NULL. I've removed all my custom routing to make sure there's nothing I've overridden to make it so.

Example: URL: https://localhost/user/createItem $dispatcher->getActionName() = "createItem"

URL: https://localhost/user/create-item $dispatcher->getActionName() = NULL

What am I doing wrong?

I'm runnig Apache2 on Mac OSX Mountain Lion.

Thanks.

// dimhoLt

EDIT: I solved the above issue temporarily by adding the following route:


        $router->add(
            "/([a-zA-Z\-]+)/([a-zA-Z\-]+)",
            array(
                "controller" => 1,
                "action"     => 2
            )
        );

But firstly, this seems like the wrong way to do it since /controller/action is implicit, and secondly, the correct action is still not executed (action names have lower-case first characters, so I set the action name to lcfirst(\Phalcon\Text::camelize($dispatcher->getActionName())), should be correct).

Any assistance is appreciated. Thank you.



98.9k
Accepted
answer


22.6k

Yeah, that's exactly the same instruction I showed a link to... I get NULL from action names if they're not already camelized...



1.2k

Agreed. I also get NULL for the action name when then action is hyphenated. The router won't match an action with a - in it, so there is no action to camelize. At least that's what I see in my tests.



22.6k

@jelofson Apparently, if you don't actively route anything, the path https://mysite.local/my-epic/super-page will implicitly route to MyEpicController::superPageAction(). However, it appears this doesn't work in earlier versions of Phalcon, I have not delved deeper into which version, but in 1.3.4 that I'm currently using, it does work implicitly. Try to just remove your own implementation and have Phalcon handle it silently, and it should work.



1.2k

I still am not convinced this works out-of-the-box with the default route matching. Action names with a hyphen (-) don't get matched. I just set up a vanilla project in 1.3.4 and you won't get any matching on an action with a - in it. The default route pattern for an action is: ([a-zA-Z0-9_]+)

Therefore, you can't get the $dispatcher->getActionName() to camelize.

I used https://localhost/woop/test/hello-world where woop is the base and test is the controller.



1.2k

Here is a good explanation. https://github.com/phalcon/cphalcon/issues/284

Not sure why this isn't the default behaviour.