Example #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);
 }
Example #2
0
 /**
  * @test
  */
 public function to_sends_request()
 {
     $request = new Request('http://localhost/path?query=yes', 'GET');
     $url = 'https://www.example.com';
     $adapter = $this->getMockBuilder('Proxy\\Adapter\\Dummy\\DummyAdapter')->getMock();
     $verifyParam = $this->callback(function (RequestInterface $request) use($url) {
         return $request->getUri() == 'https://www.example.com/path?query=yes';
     });
     $adapter->expects($this->once())->method('send')->with($verifyParam)->willReturn(new Response());
     $proxy = new Proxy($adapter);
     $proxy->forward($request)->to($url);
 }