Esempio n. 1
0
 /**
  * @param $services
  */
 public function __construct($services)
 {
     require_once __DIR__ . '/FredyAutoloader.php';
     $router = new Router($_SERVER);
     /**
      * Main logic of the framework.
      * Fetch the controller and method from the router, create the object with it's dependencies and call the method.
      * If there's no Controller/Method which matches a PageNotFoundException is thrown by the router.
      *
      * You can use the PageNotFoundException also in your controller to display a page not found page.
      *
      * It's also possible to use the ServerErrorException for 500 Server codes.
      */
     try {
         $request = $router->getRequest();
         $controller = $services[$request->controllerName];
         $response = $this->callAction($controller, $request->actionName, $request);
     } catch (PageNotFoundException $e) {
         /**
          * @var $controller \Controller\Error
          */
         $controller = $services[$e->getController()];
         $controller->setErrorMessage($e->getMessage());
         $response = $this->callAction($controller, $e->getAction(), new Request(null, null, null, null));
     } catch (ServerErrorException $e) {
         $controller = $services[$e->getController()];
         $response = $this->callAction($controller, $e->getAction(), new Request(null, null, null, null));
     }
     echo $response->render();
 }
Esempio n. 2
0
 public function testRouting()
 {
     $serverOptions = $_SERVER;
     $serverOptions['REQUEST_URI'] = '/';
     $serverOptions['REQUEST_METHOD'] = 'GET';
     $serverOptions['SERVER_PROTOCOL'] = 'http';
     $serverOptions['SERVER_PORT'] = '80';
     $serverOptions['SERVER_NAME'] = 'test';
     $router = new Router($serverOptions, 'Test/test.yaml');
     $request = $router->getRequest();
     $this->assertStringMatchesFormat('demo', $request->controllerName);
     $router->requestURL = substr('/', strlen(OFFSETPATH));
     $request = $router->getRequest();
     $this->assertStringMatchesFormat('demo', $request->controllerName);
 }