Skip to content

Instantly share code, notes, and snippets.

@michael34435
Created April 4, 2015 03:22
Show Gist options
  • Save michael34435/8e097e4edde5c66900bf to your computer and use it in GitHub Desktop.
Save michael34435/8e097e4edde5c66900bf to your computer and use it in GitHub Desktop.
camelize your action name in phalcon
<?php
use Phalcon\Text;
use Phalcon\Mvc\Router;
use Phalcon\Mvc\Dispatcher as MvcDispatcher;
use Phalcon\Events\Manager as EventsManager;
$di->set('dispatcher', function() {
$eventsManager = new EventsManager();
$eventsManager->attach('dispatch:afterDispatchLoop', function($event, $dispatcher) {
$actionNameFirstPart = substr($dispatcher->getActionName(), 0, 1);
$actionNameSecondPart = substr($dispatcher->getActionName(), 1);
$actionNameSecondPart = preg_replace('/([A-Z])/', '-$1', $actionNameSecondPart);
$actionName = $actionNameFirstPart . $actionNameSecondPart;
$actionName = strtolower($actionName);
$dispatcher->setActionName($actionName);
});
$dispatcher = new MvcDispatcher();
$dispatcher->setEventsManager($eventsManager);
return $dispatcher;
});
$di->set('router', function() {
$router = new Router();
$router->add('/([a-zA-Z\-]+)/([a-zA-Z\-]+)/:params', array(
'controller' => 1,
'action' => 2,
'params' => 3
))->convert('action', function($action) {
return lcfirst(Text::camelize($action));
});
return $router;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment