Created
February 14, 2014 15:22
-
-
Save ryanwalters/9002906 to your computer and use it in GitHub Desktop.
Phalcon 1.2.6: $dispatcher->getControllerName() not returning a string?
This file contains 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 | |
// /public/index.php | |
try { | |
// Read the configuration | |
$config = new Phalcon\Config\Adapter\Ini('../app/config/config.ini'); | |
// Register an autoloader | |
$loader = new Phalcon\Loader(); | |
$loader->registerDirs(array( | |
$config->application->controllersDir, | |
$config->application->modelsDir, | |
$config->application->pluginsDir | |
)); | |
$loader->register(); | |
// Create a DI | |
$di = new Phalcon\DI\FactoryDefault(); | |
// Setup the view component | |
$di->set('view', function () use ($config) { | |
$view = new Phalcon\Mvc\View(); | |
$view->setViewsDir($config->application->viewsDir); | |
$view->registerEngines(array( | |
'.volt' => 'volt' | |
//'.volt' => 'Phalcon\Mvc\View\Engine\Volt' | |
)); | |
return $view; | |
}); | |
// Set up volt templating | |
$di->set('volt', function ($view, $di) use ($config) { | |
$volt = new Phalcon\Mvc\View\Engine\Volt($view, $di); | |
$volt->setOptions(array( | |
'compiledPath' => $config->application->cacheDir.'volt/' | |
)); | |
return $volt; | |
}); | |
// Setup a base URI so that all generated URIs include the 'phalcon' folder | |
$di->set('url', function () use ($config) { | |
$url = new Phalcon\Mvc\Url(); | |
$url->setBaseUri($config->application->baseUri); | |
return $url; | |
}); | |
// Register the events manager | |
$di->set('dispatcher', function () use ($di) { | |
// Obtain the standard eventsManager from the DI | |
$eventsManager = new Phalcon\Events\Manager(); | |
$dispatcher = new Phalcon\Mvc\Dispatcher(); | |
// Attach a listener for type "dispatch" | |
$eventsManager->attach('dispatch', function ($event, $dispatcher) { | |
$controllerName = Phalcon\Text::camelize($dispatcher->getControllerName()); | |
$dispatcher->setControllerName($controllerName); | |
$actionName = Phalcon\Text::camelize($dispatcher->getActionName()); | |
$dispatcher->setActionName($actionName); | |
}); | |
// Bind the EventsManager to the Dispatcher | |
$dispatcher->setEventsManager($eventsManager); | |
return $dispatcher; | |
}); | |
// Register route patterns | |
$di->set('router', function () { | |
$router = new Phalcon\Mvc\Router(false); | |
$router->add('/:controller', array( | |
'controller' => 1 | |
)); | |
$router->add('/:controller/([a-zA-Z\-]+)/:params', array( | |
'controller' => 1, | |
'action' => 2, | |
'params' => 3 | |
)); | |
return $router; | |
}); | |
// Handle the request | |
$application = new Phalcon\Mvc\Application($di); | |
echo $application->handle()->getContent(); | |
} catch (Phalcon\Excpetion $e) { | |
echo 'PhalconException: ', $e->getMessage(); | |
} catch (PDOException $e) { | |
echo 'PDOException: ', $e->getMessage(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment