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

camelCase Actions

Is there any way to write a controller action like this "anExampleAction" (camelCase) and access as "module/controller/an-example"?

You can camelize the action before be dispatched using a plugin:

$di['dispatcher'] = function(){

    $eventsManager = new Phalcon\Events\Manager();

    $eventsManager->attach('dispatch', function($event, $dispatcher) {
        if ($event->getType() == 'beforeDispatchLoop') {            
            $dispatcher->setActionName(Phalcon\Text::camelize($dispatcher->getActionName()));
        }
    });

    $dispatcher = new Phalcon\MVc\Dispatcher();
    $dispatcher->setEventsManager($eventsManager);

    return $dispatcher;
});


1.2k

Hi There, I just stumbled on this and I have a feeling that in 1.3.4 this won't work as expected.

Assume a URL like https://example.com/index/edit-comment where index is the controller and edit-comment is the action.

// In the config/services.php file
$di->set('dispatcher', function(){

    $eventsManager = new Phalcon\Events\Manager();

    $eventsManager->attach('dispatch', function($event, $dispatcher) {
        if ($event->getType() == 'beforeDispatchLoop') {      
            // $dispatcher->getActionName() will be NULL and a warning issued on camelize()
            $dispatcher->setActionName(Phalcon\Text::camelize($dispatcher->getActionName()));
        }

    });

    $dispatcher = new Phalcon\MVc\Dispatcher();
    $dispatcher->setEventsManager($eventsManager);

    return $dispatcher;
});

In this case, the getActionName() will return NULL. Unless there is a better place for this?

Is there a new way to accomplish this? I would like to use camelCase actions using the default routes.

v2.0 also won't work , getActionName() return empty

Hi There, I just stumbled on this and I have a feeling that in 1.3.4 this won't work as expected.

Assume a URL like https://example.com/index/edit-comment where index is the controller and edit-comment is the action.

// In the config/services.php file
$di->set('dispatcher', function(){

   $eventsManager = new Phalcon\Events\Manager();

   $eventsManager->attach('dispatch', function($event, $dispatcher) {
       if ($event->getType() == 'beforeDispatchLoop') {      
           // $dispatcher->getActionName() will be NULL and a warning issued on camelize()
           $dispatcher->setActionName(Phalcon\Text::camelize($dispatcher->getActionName()));
       }

   });

   $dispatcher = new Phalcon\MVc\Dispatcher();
   $dispatcher->setEventsManager($eventsManager);

   return $dispatcher;
});

In this case, the getActionName() will return NULL. Unless there is a better place for this?

Is there a new way to accomplish this? I would like to use camelCase actions using the default routes.

edited May '15

find another way: $router = new Router(false);

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