Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

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: http://localhost/user/createItem $dispatcher->getActionName() = "createItem"

URL: http://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.



83.0k
Accepted
answer


14.0k

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

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.



14.0k

@jelofson Apparently, if you don't actively route anything, the path http://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.

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 http://localhost/woop/test/hello-world where woop is the base and test is the controller.

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

Not sure why this isn't the default behaviour.