コード例 #1
0
ファイル: Controller.php プロジェクト: janusnic/OctoberCMS
 /**
  * Sets standard page variables in the case of a controller error.
  */
 public function handleError($exception)
 {
     $errorMessage = ApplicationException::getDetailedMessage($exception);
     $this->fatalError = $errorMessage;
     $this->vars['fatalError'] = $errorMessage;
 }
コード例 #2
0
ファイル: Controller.php プロジェクト: janusnic/OctoberCMS
 /**
  * Executes the page, layout, component and plugin AJAX handlers.
  * @return mixed Returns the AJAX Response object or null.
  */
 protected function execAjaxHandlers()
 {
     if ($handler = trim(Request::header('X_OCTOBER_REQUEST_HANDLER'))) {
         try {
             /*
              * Validate the handler name
              */
             if (!preg_match('/^(?:\\w+\\:{2})?on[A-Z]{1}[\\w+]*$/', $handler)) {
                 throw new CmsException(Lang::get('cms::lang.ajax_handler.invalid_name', ['name' => $handler]));
             }
             /*
              * Validate the handler partial list
              */
             if ($partialList = trim(Request::header('X_OCTOBER_REQUEST_PARTIALS'))) {
                 $partialList = explode('&', $partialList);
                 foreach ($partialList as $partial) {
                     if (!preg_match('/^(?:\\w+\\:{2}|@)?[a-z0-9\\_\\-\\.\\/]+$/i', $partial)) {
                         throw new CmsException(Lang::get('cms::lang.partial.invalid_name', ['name' => $partial]));
                     }
                 }
             } else {
                 $partialList = [];
             }
             $responseContents = [];
             /*
              * Execute the handler
              */
             if (!($result = $this->runAjaxHandler($handler))) {
                 throw new CmsException(Lang::get('cms::lang.ajax_handler.not_found', ['name' => $handler]));
             }
             /*
              * If the handler returned an array, we should add it to output for rendering.
              * If it is a string, add it to the array with the key "result".
              */
             if (is_array($result)) {
                 $responseContents = array_merge($responseContents, $result);
             } elseif (is_string($result)) {
                 $responseContents['result'] = $result;
             }
             /*
              * Render partials and return the response as array that will be converted to JSON automatically.
              */
             foreach ($partialList as $partial) {
                 $responseContents[$partial] = $this->renderPartial($partial);
             }
             /*
              * If the handler returned a redirect, process it so framework.js knows to redirect
              * the browser and not the request!
              */
             if ($result instanceof RedirectResponse) {
                 $responseContents['X_OCTOBER_REDIRECT'] = $result->getTargetUrl();
             }
             return Response::make($responseContents, $this->statusCode);
         } catch (ValidationException $ex) {
             /*
              * Handle validation errors
              */
             $responseContents['X_OCTOBER_ERROR_FIELDS'] = $ex->getFields();
             $responseContents['X_OCTOBER_ERROR_MESSAGE'] = $ex->getMessage();
             return Response::make($responseContents, 406);
         } catch (Exception $ex) {
             return Response::make(ApplicationException::getDetailedMessage($ex), 500);
         }
     }
     return null;
 }