Example #1
0
 /**
  * Route the request and execute the controller.
  */
 public static function process()
 {
     Request::init();
     $route = Router::process();
     if (!$route) {
         $route = Router::getRoute('404');
     }
     if ($route) {
         list($class, $method) = explode('::', $route->controller);
         $action = "{$method}Action";
         Request::$properties->set(['controller' => $class, 'action' => $method]);
         if (!class_exists($class)) {
             throw new Exception("Controller class [{$class}] not found");
         }
         if (!method_exists($class, $action)) {
             throw new Exception("Controller action [{$route->controller}Action] not found");
         }
         $controller = new $class();
         $response = static::runFilters('before', $controller, $method);
         if (!$response) {
             $response = call_user_func_array([$controller, $action], $route->actionParams());
         }
         static::runFilters('after', $controller, $method);
         if (!$response instanceof Response) {
             throw new Exception("The controller returned an invalid response");
         }
         return $response;
     } else {
         throw new Exception(sprintf("No route matches [%s %s] and no 404 controller set", Request::$method, Request::$pathInfo));
     }
 }
Example #2
0
 public function __construct($path = '/', array $requestInfo = [])
 {
     $requestInfo = $requestInfo + ['method' => "GET", 'post' => [], 'get' => [], 'cookie' => []];
     $_SERVER['HTTP_HOST'] = "localhost";
     $_SERVER['REQUEST_METHOD'] = $requestInfo['method'];
     $_SERVER['REQUEST_URI'] = $path;
     $_SERVER['QUERY_STRING'] = '';
     $_POST = $requestInfo['post'];
     $_GET = $requestInfo['get'];
     $_REQUEST = array_merge($_GET, $_POST);
     $_COOKIE = $requestInfo['cookie'];
     Request::reset();
     Request::init();
 }