Is there any way to write a controller action like this "anExampleAction" (camelCase) and access as "module/controller/an-example"?
|
May '15 |
4 |
1913 |
1 |
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;
});
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.