Example #1
0
 /**
  * @return Response
  * @throws MvcException
  * @throws \Dez\EventDispatcher\Exception
  * @throws \Exception
  */
 public function run()
 {
     $this->event->dispatch(MvcEvent::ON_BEFORE_APP_RUN, new MvcEvent($this));
     $router = $this->initializeDefaultRoutes()->handle();
     if ($router->isFounded()) {
         $resolver = new ControllerResolver($this->getDi());
         $namespace = null === $router->getNamespace() ? $this->getControllerNamespace() : $router->getNamespace();
         $resolver->setNamespace($namespace);
         $resolver->setController($router->getController());
         $resolver->setAction($router->getAction());
         $resolver->setParams($router->getMatches());
         try {
             $response = $resolver->execute();
             $content = $response->getControllerContent();
             $controller = $response->getControllerInstance();
             $this->prepareView();
             if (null === $content && $this->response->getBodyFormat() == Response::RESPONSE_HTML) {
                 $templatePath = "{$resolver->getController()}/{$resolver->getAction()}";
                 $content = $this->render($templatePath, $controller->getPseudoPath());
             }
             if (null !== $controller->getLayout()) {
                 $content = $this->render($controller->getLayout(), null, ['content' => $content]);
             }
             $this->response->setContent($content);
             $this->event->dispatch(MvcEvent::ON_AFTER_APP_RUN, new MvcEvent($this));
         } catch (Page404Exception $exception) {
             $this->response->setStatusCode(404);
             throw $exception;
         } catch (\Exception $exception) {
             $this->event->dispatch(MvcEvent::ON_DISPATCHER_ERROR, new MvcEvent($this));
             $this->response->setStatusCode(500);
             throw $exception;
         }
     } else {
         $this->event->dispatch(MvcEvent::ON_PAGE_404, new MvcEvent($this));
         $this->response->setStatusCode(404);
         throw new Page404Exception("Page with route '{$router->getTargetUri()}' was not found");
     }
     return $this->response->send();
 }
Example #2
0
 /**
  * @param array $parameters
  * @param bool $render
  * @return mixed
  * @throws MvcException
  */
 public function execute(array $parameters = [], $render = false)
 {
     if (!isset($parameters['action'])) {
         throw new MvcException("Action required for forwarding");
     }
     $resolver = new ControllerResolver($this->getDi());
     $resolver->setNamespace(isset($parameters['namespace']) ? $parameters['namespace'] : $this->getNamespace());
     $resolver->setController(isset($parameters['controller']) ? $parameters['controller'] : $this->getName());
     if (isset($parameters['params'], $parameters['params'][0])) {
         $resolver->setParams($parameters['params']);
     }
     $resolver->setAction($parameters['action']);
     $response = $resolver->execute();
     $content = $response->getControllerContent();
     if ($render === true) {
         $content = $this->view->render("{$resolver->getController()}/{$resolver->getAction()}");
     }
     return $content;
 }