Пример #1
0
 /**
  * Instantiate the controller, execute it and return a response.
  * @param $request Request the request.
  * @return  $response Response to send.
  * @throws ControllerNotExist The controller doesn't exist.
  * @throws PageNotExist No route found for the request uri.
  */
 public function execute(Request $request)
 {
     $method = $request->getMethod();
     $argPos = strpos($request->getUri(), '?');
     if ($argPos !== false && $argPos >= 0) {
         $uri = substr($request->getUri(), 0, $argPos);
     } else {
         $uri = $request->getUri();
     }
     $route = self::getRoute($method, $uri);
     if (!$route) {
         throw new PageNotExist('No route found');
     }
     $path = explode('@', $route['path']);
     $controller_name = $path[0];
     $action = $path[1];
     $args = $route['args'];
     // Try to instantiate the controller
     $controller = Config::$app['namespace'] . 'Controllers\\' . $controller_name;
     if (class_exists($controller)) {
         $controller = new $controller($request);
     } else {
         throw new ControllerNotExist('Controller "' . $controller . '" doesn\'t exist !');
     }
     return $controller->execute($action, $args);
 }