Example #1
0
 /**
  * When a Controller has been found, the handleRequest method will be invoked, 
  * which is responsible for handling the actual request and - if applicable - returning 
  * an appropriate Response. So actually, this method is the main entrypoint for the 
  * dispatch loop which delegates requests to controllers. 
  * 
  * @param string $controllerName
  * @param string $actionName 
  * @return mixed
  * @throws RequestException
  * @throws RuntimeException
  */
 public function handleRequest($controllerName, $actionName)
 {
     try {
         $controller = Loader::getInstance()->getController($controllerName);
     } catch (LoaderException $e) {
         throw new RequestException($e->getMessage(), Response::NOT_FOUND);
     }
     if (method_exists($controller, 'setRequest')) {
         $controller->setRequest($this);
     }
     $controllerResponse = null;
     if (method_exists($controller, 'init')) {
         $controllerResponse = $controller->init($this);
     }
     if (!isset($controllerResponse)) {
         if (!method_exists($controller, $actionName . 'Action')) {
             $m = sprintf('Method "%s" not found', $actionName);
             throw new RequestException($m, Response::NOT_FOUND);
         }
         $controllerResponse = $controller->{$actionName . 'Action'}($this);
     }
     $this->setController($controllerName);
     $this->setAction($actionName);
     if ($controllerResponse instanceof View) {
         $this->setContentType('html');
         $response = new Response();
         $response->setView($controllerResponse);
         return $response;
     } else {
         if ($controllerResponse instanceof Response) {
             return $controllerResponse;
         } else {
             $m = sprintf('Method "%s::%sAction()" contains an invalid return type', $controllerName, $actionName);
             throw new RuntimeException($m);
         }
     }
 }
Example #2
0
 /**
  * @param string $tpl
  *
  * @return $this
  * @see Response::setView
  */
 public function setView(string $tpl)
 {
     $this->response->setView($tpl);
     return $this;
 }