Example #1
0
 /**
  * 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;
                 }
             }
         }
     }
 }