public function __construct($routes) { $router_factory = new RouterFactory(); $this->_router = $router_factory->newInstance(); // home route $this->_router->addGet('home', '/'); // routes from config foreach ($routes as $name => $values) { foreach ($values['actions'] as $action) { $method = 'add' . Utils::firstToUpper($action['verb']); $this->_router->{$method}($name . '.' . $action['name'], '/' . $values['prefix'] . $action['pattern'])->addTokens(array('id' => '\\d+')); } } }
public function dispatch($route) { if (empty($route)) { $this->_errorController->error('404 unknown controller'); } else { $className = Utils::firstToUpper($route['controller']) . 'Controller'; $controller = new $className(); $action = 'action' . Utils::firstToUpper($route['action']); try { $controller->{$action}($route['id']); } catch (Exception $e) { $errorController = new \ErrorController(); $this->_errorController->error($e->getMessage()); } } }
protected function __construct($config = array()) { parent::__construct(); $this->_values = new Container(); $routerClass = Utils::checkInterfaceImplementation($config['routerClass'], 'PollExample\\RouterInterface'); $routes = $config['routes'] ?: array(); $this->_values['router'] = function ($c) use($routerClass, $routes) { return new $routerClass($routes); }; $dispatcherClass = Utils::checkInterfaceImplementation($config['dispatcherClass'], 'PollExample\\DispatcherInterface'); $this->_values['dispatcher'] = function ($c) use($dispatcherClass, $config) { return new $dispatcherClass($config['appDir'] . '/' . $config['controllersDir']); }; $entityManagerClass = Utils::checkInterfaceImplementation($config['entityManagerClass'], 'PollExample\\EntityManagerInterface'); $this->_values['entityManager'] = function ($c) use($entityManagerClass, $config) { $entityManager = new $entityManagerClass($config['db'], $config['appDir'] . '/' . $config['modelsDir'], $config['debug']); return $entityManager->create(); }; $templateEngineClass = Utils::checkInterfaceImplementation($config['templateEngineClass'], 'PollExample\\TemplateEngineInterface'); $this->_values['templateEngine'] = function ($c) use($templateEngineClass, $config) { return new $templateEngineClass($config['appDir'] . '/' . $config['viewsDir']); }; }