protected function _default(Request $request) { $route = new Route(); $uri = $request->getUri(); $uri = strpos($uri, "?") !== false ? substr($uri, 0, strpos($uri, "?")) : $uri; $parts = explode("/", $uri); $parts = $this->_filter($parts); switch (count($parts)) { case 0: $route->setControllerName("index"); $route->setActionName("index"); break; case 1: $route->setControllerName($parts[0]); $route->setActionName("index"); array_shift($parts); break; default: $route->setControllerName($parts[0]); $route->setActionName($parts[1]); array_shift($parts); array_shift($parts); break; } count($parts) % 2 !== 0 ? array_pop($parts) : false; if (count($parts)) { for ($i = 0; $i < count($parts); $i = $i + 2) { $request->addParam($parts[$i], $parts[$i + 1]); } } return $route; }
public function match(Request $request, $route = false) { if ($request->getUri() == $this->_path) { $static = new Route(); $static->setControllerName($this->_controller); $static->setActionName($this->_action); $static->merge($route); return parent::match($request, $static); } }
public function dispatch(Route $route) { $protoView = $this->getBootstrap()->getResource("view") ? $this->getBootstrap()->getResource("view") : new View(); $controllerPath = $this->getControllerPath(); $router = $this->_router; $request = $this->_request; $protoView->addHelper("pull", function ($uri) use($controllerPath, $router, $request) { $request = clone $request; $request->setUri($uri); $routeObj = $router->match($request); $controllerClassName = $routeObj->getControllerName() . "Controller"; $action = $routeObj->getActionName() . "Action"; $classPath = realpath($controllerPath . DIRECTORY_SEPARATOR . $controllerClassName . ".php"); if (file_exists($classPath)) { require_once $classPath; $controller = new $controllerClassName(); $controller->setParams($routeObj->getParams()); if (method_exists($controller, $action)) { ob_start(); $controller->init(); $data = $controller->{$action}(); ob_end_clean(); return $data; } else { throw new RuntimeException("Pull operation {$routeObj->getControllerName()} - {$routeObj->getActionName()} failed.", 404); } } else { throw new RuntimeException("Pull operation {$routeObj->getControllerName()} - {$routeObj->getActionName()} failed.", 404); } }); $dispatcher = new Dispatcher($protoView); $dispatcher->setRouter($this->_router); $dispatcher->setRequest($this->_request); $dispatcher->setEventManager($this->getEventManager()); $dispatcher->setBootstrap($this->_bootstrap); $dispatcher->setControllerPath($this->getControllerPath()); try { $this->_page = $dispatcher->dispatch($route); } catch (Exception $e) { $errorRoute = new Route(); $errorRoute->addParams(array('exception' => $e)); $dispatcher->clearHeaders(); $dispatcher->addHeader("", "", 404); $errorRoute->setControllerName("error"); $errorRoute->setActionName("error"); $this->_page = $dispatcher->dispatch($errorRoute); } return array('headers' => $dispatcher->getHeaders()); }