Example #1
0
 private function invokeControllerMethod()
 {
     $pathInfo = self::getPathInfo();
     $this->timeLogger->setText($pathInfo);
     $ctx = $this->getContext();
     $router = $this->loadRouter();
     $ctx->setRouter($router);
     $route = $router->match($pathInfo, $_SERVER);
     // If no route found, show a 404
     if (!$route) {
         throw new PageNotFoundException(Application::getTranslator()->_("Invalid URL."));
     }
     list($controllerAlias, $methodName, $lang, $redirectPath) = $this->getRouteResult($route);
     Logger::debug("Route: " . $route->name . " (controller={$controllerAlias}, method={$methodName}, lang={$lang}, redirect={$redirectPath})");
     $controllerClassName = null;
     try {
         $controllerClassName = $this->controllerClassNameFromAlias($controllerAlias);
     } catch (IllegalArgumentException $e) {
         throw new PageNotFoundException($e->getMessage(), 0, $e);
     }
     $ctx->setControllerAlias($controllerAlias);
     if (!class_exists($controllerClassName)) {
         throw new PageNotFoundException("Controller class not found: {$controllerClassName}");
     }
     $controller = new $controllerClassName();
     // Check if access is allowed. Controller will redirect if not.
     // TODO: Show a 403 if no access allowed
     if (!$controller->checkAccess($ctx)) {
         header('HTTP/1.1 403 Forbidden');
         return;
     }
     // If locale is required and set, but does not exist throw 404 error
     if ($controller->isLocaleSupported() && $lang && !in_array($lang, self::$translator->getAvailableLocales())) {
         throw new PageNotFoundException(Application::getTranslator()->_("Invalid URL."));
     }
     $locale = $this->getLocale($ctx, $controller, $lang);
     $supportedLocale = $this->getSupportedLocale($locale);
     /**
      * Support the 'redirect' directive of the route.
      * If the route included a 'redirect' value, we redirect to that path,
      * passing all route values + 'lang'.
      */
     if ($redirectPath) {
         $data = array_merge($route->params, array('lang' => $supportedLocale));
         $url = '/' . $router->generate($redirectPath, $data);
         // Include query string when redirecting
         $qs = $_SERVER['QUERY_STRING'];
         if ($qs) {
             $url .= "?{$qs}";
         }
         $ctx->redirect($url, true);
     }
     I18nUtil::setDefaultLocale($supportedLocale);
     self::$translator->setLocale($supportedLocale);
     header('Content-Language: ' . self::$translator->getLocale());
     // Allow dashes in method name (for SEO purposes). Converts to camelCase.
     $methodName = $this->camelize($methodName);
     if (!method_exists($controller, $methodName)) {
         throw new PageNotFoundException("Missing action method '{$methodName}' in controller {$controllerClassName}");
     }
     $view = null;
     // Invoke the controller's method
     try {
         $view = $controller->{$methodName}($ctx);
     } catch (ForwardViewException $e) {
         // Hanlde 'forwarding': A controller method threw this exception
         // containing a view instead of returning it in a normal way.
         $view = $e->getView();
     }
     if ($view instanceof View) {
         if ($ctx->getUIManager()->getErrorManager()->hasErrors()) {
             $ctx->getForm()->setValues($ctx->getAttributes());
         }
         if (self::$translator) {
             $view->setTranslator(self::$translator);
         }
         $view->init($ctx);
         global $form;
         $view->render($ctx);
     }
 }
Example #2
0
 public static function setDefaultLocale($locale)
 {
     self::$bundleLocale = (string) $locale;
 }