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.

how to set controllers and actions in routes with dashes

Hi, I am considering moving a site to phalcon. The problem is the part of the url where the controller and action would be has dashes in many of the urls. I think I cannot have dashes in the controller names and action names. How can I get around this problem?
thanks! -Les



13.0k
Accepted
answer

Maybe this code will help a little. Add this to your routes.php to make your url yourcontroller/your-long-action readable by your controller

<?php

$router = new Phalcon\Mvc\Router();

$router->add('/([a-zA-Z\-]+)/([a-zA-Z\-]+)/:params', array(
    'controller' => 1,
    'action' => 2,
    'params' => 3
))->convert('action', function($action) {
    return Phalcon\Text::lower(Phalcon\Text::camelize($action));
});

return $router;

Awesome! I understand your solution well. I will try to implement it tonight or tomorrow. Thank you very much!

Cool, never seen the convert function before. The only thing I would change is the return:

return lcfirst(Phalcon\Text::camelize($action));

This would keep with PSR-1 coding standards that all methods names must be declared in camelCase. Using Phalcon\Text::lower() would make the entire name lowercase.

Hi again, I am embarrassed to ask, but where does this code go? I have a modification of the INVO app, and there is no routers section there (as opposed to the Vokuro app which has a routers section). I added it to the index.php in public, in various places, all of which cause the app to crash.

My index.php file is virtually unchanged from INVO (except that I added a few lines of assets near the bottom).

Thanks again! -Les

I know, its a bit late - the code goes to your services.php ...

Timo

thanks a lot.

I did a bit of digging around a while ago and found one pattern (I think it was in the vokuro example) that put it in apps/config/routes.php so I made that file and put it there. It seems to work so far.

I probably should have put that answer here too. Sorry for that and thanks for the help! -Les



65

this is what i looking for!!

Maybe this code will help a little. Add this to your routes.php to make your url yourcontroller/your-long-action readable by your controller

```php <?php

$router = new Phalcon\Mvc\Router();

$router->add('/([a-zA-Z-]+)/([a-zA-Z-]+)/:params', array( 'controller' => 1, 'action' => 2, 'params' => 3 ))->convert('action', function($action) { return Phalcon\Text::lower(Phalcon\Text::camelize($action)); });

return $router; ```