Created
April 4, 2015 03:22
-
-
Save michael34435/8e097e4edde5c66900bf to your computer and use it in GitHub Desktop.
camelize your action name in phalcon
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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