Ejemplo n.º 1
0
 /**
  * @param ApplicationInterface $app
  * @param string $controllerStr
  * @param array $additionalParams
  * @param string $actionStr
  * @return ControllerInterface|mixed|null
  * Create controller from system path e.g. controller/action.
  * Add additionalParams for well.. additional parameters.
  * actionStr is needed to load language for that action.
  */
 public static function create(ApplicationInterface $app, $controllerStr = '', $actionStr = null, $additionalParams = array())
 {
     $str_r = explode('\\', $controllerStr);
     if (!empty($str_r)) {
         foreach ($str_r as $index => $str) {
             $str = ucfirst($str);
             $str_r[$index] = $str;
         }
         $controllerStr = implode('\\', $str_r);
     }
     if ($controllerStr[0] != '\\') {
         $controllerStr = ucfirst($controllerStr);
     } else {
         $controllerStr = ucfirst(substr($controllerStr, 1));
     }
     $controllerStrComplete = self::$config['namespace'] . '\\Controllers\\' . $controllerStr . 'Controller';
     //      Controller not found, but we try to find view anyway.
     if (!class_exists($controllerStrComplete)) {
         $path = $app->getRouter()->getControllerPathFromClass($controllerStrComplete);
         $viewPath = empty($actionStr) ? 'index' : $actionStr;
         if ($viewPath[0] == '_') {
             $url = $app->getRouter()->getUrl($path . "/{$viewPath}", $additionalParams);
             PageNotFoundException::alert($path . "/{$viewPath}", $url);
         } else {
             try {
                 $completeViewPath = $path . DIRECTORY_SEPARATOR . $viewPath . '.tpl';
                 $app->getTemplateEngine()->display($completeViewPath);
             } catch (InvalidTemplateException $e) {
                 $url = $app->getRouter()->getUrl($path . "/{$viewPath}", $additionalParams);
                 PageNotFoundException::alert($path . "/{$viewPath}", $url);
             }
         }
     } else {
         /* @var $controller ControllerInterface */
         $controller = new $controllerStrComplete($app, $actionStr, $additionalParams);
         return $controller;
     }
     return null;
 }
Ejemplo n.º 2
0
 /**
  * @param $action
  * @return mixed
  */
 public function runAction($action)
 {
     $value = null;
     $this->setCurrentAction($action);
     $this->stopAction = false;
     $this->beforeAction();
     if (!$this->stopAction) {
         if (method_exists($this, $action)) {
             $value = $this->{$action}();
         } else {
             //          Action not found, but we check to see if view is available anyway.
             if ($action[0] == '_') {
                 $url = $this->app->getRouter()->getUrl($this->getControllerPath() . "/{$action}", $this->getParams());
                 PageNotFoundException::alert($this->getControllerPath() . "/{$action}", $url);
             } else {
                 try {
                     $this->render($action);
                 } catch (InvalidTemplateException $e) {
                     $url = $this->app->getRouter()->getUrl($this->getControllerPath() . "/{$action}", $this->getParams());
                     if (strpos($e->getMessage(), 'Unable to load template file') !== false) {
                         PageNotFoundException::alert($this->getControllerPath() . "/{$action}", $url);
                     } else {
                         throw $e;
                     }
                 }
             }
         }
         $this->afterAction();
     } else {
         $this->stopAction = false;
     }
     return $value;
 }