/** * Route to the correct controller * * @throws Exception * @return void */ public function route() { if ($this->routeMatch->match()) { if ($this->routeMatch->hasController()) { $controller = $this->routeMatch->getController(); if ($controller instanceof \Closure) { $this->controllerClass = 'Closure'; $this->controller = $controller; } else { if (class_exists($controller)) { $this->controllerClass = $controller; $controllerParams = null; if ($this->routeMatch->hasControllerParams($controller)) { $controllerParams = $this->routeMatch->getControllerParams($controller); } else { if ($this->routeMatch->hasControllerParams('*')) { $controllerParams = $this->routeMatch->getControllerParams('*'); } } if (null !== $controllerParams) { $this->controller = (new \ReflectionClass($controller))->newInstanceArgs($controllerParams); } else { $this->controller = new $controller(); } if (!$this->controller instanceof \Pop\Controller\ControllerInterface) { throw new Exception('Error: The controller must be an instance of Pop\\Controller\\Interface'); } $action = $this->routeMatch->getAction(); $this->action = null === $action && $this->routeMatch->isDynamicRoute() ? 'index' : $action; } } } } }
/** * Route to the correct controller * * @return void */ public function route() { $this->match(); $controller = $this->routeMatch->getController(); if (null === $controller) { $controller = $this->routeMatch->getDefaultController(); } if (null !== $controller) { // If controller is a closure if ($controller instanceof \Closure) { $this->controller = $controller; $this->controllerClass = $controller; if ($this->routeMatch->hasDispatchParams()) { $this->addDispatchParams($this->routeMatch->getRoute(), $this->routeMatch->getDispatchParams()); } // Else if controller is a class } else { if (class_exists($controller)) { $this->controllerClass = $controller; // If parameters are found, add them for dispatch if ($this->routeMatch->hasControllerParams()) { $this->addControllerParams($controller, $this->routeMatch->getControllerParams()); } $controllerParams = []; if (isset($this->controllerParams[$controller])) { $controllerParams = $this->controllerParams[$controller]; } else { if (isset($this->controllerParams['*'])) { $controllerParams = $this->controllerParams['*']; } } // If the controller has parameters if (is_array($controllerParams) && count($controllerParams) > 0) { $reflect = new \ReflectionClass($controller); $this->controller = $reflect->newInstanceArgs($controllerParams); // Else, just instantiate the controller } else { $this->controller = new $controller(); } if ($this->routeMatch->hasDispatchParams() && null !== $this->routeMatch->getAction()) { $this->addDispatchParams($this->routeMatch->getRoute(), $this->routeMatch->getDispatchParams()); } } } } }