isXmlHttpRequest() публичный статический Метод

Is the request a Javascript XMLHttpRequest?
public static isXmlHttpRequest ( ) : boolean
Результат boolean
Пример #1
0
 /**
  * Test `Is` Methods
  */
 public function testIsMethods()
 {
     $this->assertTrue(Request::isGet());
     $this->assertFalse(Request::isPost());
     $this->assertFalse(Request::isPut());
     $this->assertFalse(Request::isDelete());
     $this->assertFalse(Request::isFlashRequest());
     $this->assertFalse(Request::isXmlHttpRequest());
 }
Пример #2
0
 /**
  * {@inheritdoc}
  *
  * @throws NotImplementedException
  * @throws NotFoundException
  * @throws BadRequestException
  * @return mixed
  */
 public function __invoke()
 {
     $primary = $this->getPrimaryKey();
     // switch by method
     switch ($this->method) {
         case Request::METHOD_GET:
             $row = $this->readOne($primary);
             $result = ['row' => $row];
             if (!empty($primary)) {
                 // update form
                 $result['method'] = Request::METHOD_PUT;
             } else {
                 // create form
                 $result['method'] = Request::METHOD_POST;
             }
             break;
         case Request::METHOD_POST:
             try {
                 $result = $this->createOne($this->data);
                 if (!Request::isXmlHttpRequest()) {
                     $row = $this->readOne($result);
                     $result = ['row' => $row, 'method' => Request::METHOD_PUT];
                 }
             } catch (ValidatorException $e) {
                 $row = $this->readOne(null);
                 $row->setFromArray($this->data);
                 $result = ['row' => $row, 'errors' => $e->getErrors(), 'method' => $this->getMethod()];
             }
             break;
         case Request::METHOD_PATCH:
         case Request::METHOD_PUT:
             try {
                 $result = $this->updateOne($primary, $this->data);
                 if (!Request::isXmlHttpRequest()) {
                     $row = $this->readOne($primary);
                     $result = ['row' => $row, 'method' => $this->getMethod()];
                 }
             } catch (ValidatorException $e) {
                 $row = $this->readOne($primary);
                 $row->setFromArray($this->data);
                 $result = ['row' => $row, 'errors' => $e->getErrors(), 'method' => $this->getMethod()];
             }
             break;
         case Request::METHOD_DELETE:
             $result = $this->deleteOne($primary);
             break;
         default:
             throw new NotImplementedException();
     }
     return $result;
 }
Пример #3
0
            break;
        case 501:
            $title = __("Not Implemented");
            $description = __("The server does not understand or does not support the HTTP method");
            break;
        case 503:
            $title = __("Service Unavailable");
            $description = __("The server is currently unable to handle the request due to a temporary overloading");
            Response::setHeader('Retry-After', '600');
            break;
        default:
            $title = __("Internal Server Error");
            $description = __("An unexpected error occurred with your request. Please try again later");
            break;
    }
    // check CLI or HTTP request
    if (Request::isHttp()) {
        // simple AJAX call, accept JSON
        if (Request::getAccept(['application/json'])) {
            $this->useJson();
            Messages::addError($description);
            return null;
        }
        // dialog AJAX call, accept HTML
        if (!Request::isXmlHttpRequest()) {
            $this->useLayout('small.phtml');
        }
    }
    Layout::title($title);
    return ['error' => $title, 'description' => $description];
};
Пример #4
0
 /**
  * Do process
  * @return void
  */
 protected function doProcess()
 {
     Logger::info("app:process:do");
     $module = Request::getModule();
     $controller = Request::getController();
     $params = Request::getAllParams();
     // try to dispatch controller
     try {
         // get reflection of requested controller
         $controllerFile = $this->getControllerFile($module, $controller);
         $reflection = $this->reflection($controllerFile);
         // check header "accept" for catch JSON(P) or XML requests, and switch presentation
         // it's some magic for AJAX and REST requests
         if ($produces = $reflection->getAccept() and $accept = $this->getRequest()->getAccept()) {
             // switch statement for $accept
             switch ($accept) {
                 case Request::ACCEPT_HTML:
                     // with layout
                     // without additional presentation
                     break;
                 case Request::ACCEPT_JSON:
                     $this->useJson();
                     break;
                 case Request::ACCEPT_JSONP:
                     $this->useJsonp();
                     break;
                 case Request::ACCEPT_XML:
                     $this->useXml();
                     break;
                 default:
                     // not acceptable MIME type
                     throw new NotAcceptableException();
                     break;
             }
         }
         // check call method(s)
         if ($reflection->getMethod() && !in_array(Request::getMethod(), $reflection->getMethod())) {
             throw new NotAllowedException(join(',', $reflection->getMethod()));
         }
         // check HTML cache
         if ($reflection->getCacheHtml() && Request::getMethod() == Request::METHOD_GET) {
             $htmlKey = 'html:' . $module . ':' . $controller . ':' . http_build_query($params);
             if ($cachedHtml = Cache::get($htmlKey)) {
                 Response::setBody($cachedHtml);
                 return;
             }
         }
         // dispatch controller
         $dispatchResult = $this->dispatch($module, $controller, $params);
     } catch (RedirectException $e) {
         Response::setException($e);
         if (Request::isXmlHttpRequest()) {
             // 204 - No Content
             Response::setStatusCode(204);
             Response::setHeader('Bluz-Redirect', $e->getMessage());
         } else {
             Response::setStatusCode($e->getCode());
             Response::setHeader('Location', $e->getMessage());
         }
         return null;
     } catch (ReloadException $e) {
         Response::setException($e);
         if (Request::isXmlHttpRequest()) {
             // 204 - No Content
             Response::setStatusCode(204);
             Response::setHeader('Bluz-Reload', 'true');
         } else {
             Response::setStatusCode($e->getCode());
             Response::setHeader('Refresh', '0; url=' . Request::getRequestUri());
         }
         return null;
     } catch (\Exception $e) {
         Response::setException($e);
         // cast to valid HTTP error code
         // 500 - Internal Server Error
         $statusCode = 100 <= $e->getCode() && $e->getCode() <= 505 ? $e->getCode() : 500;
         Response::setStatusCode($statusCode);
         $dispatchResult = $this->dispatch(Router::getErrorModule(), Router::getErrorController(), array('code' => $e->getCode(), 'message' => $e->getMessage()));
     }
     if ($this->hasLayout()) {
         Layout::setContent($dispatchResult);
         $dispatchResult = Layout::getInstance();
     }
     if (isset($htmlKey, $reflection)) {
         // @TODO: Added ETag header
         Cache::set($htmlKey, $dispatchResult(), $reflection->getCacheHtml());
         Cache::addTag($htmlKey, $module);
         Cache::addTag($htmlKey, 'html');
         Cache::addTag($htmlKey, 'html:' . $module);
         Cache::addTag($htmlKey, 'html:' . $module . ':' . $controller);
     }
     Response::setBody($dispatchResult);
 }
Пример #5
0
 /**
  * Pre process
  *
  * @return void
  * @throws ApplicationException
  */
 protected function preProcess()
 {
     Router::process();
     // disable Layout for XmlHttpRequests
     if (Request::isXmlHttpRequest()) {
         $this->layoutFlag = false;
     }
     // switch to JSON response based on Accept header
     if (Request::getAccept([Request::TYPE_HTML, Request::TYPE_JSON]) == Request::TYPE_JSON) {
         $this->layoutFlag = false;
         Response::switchType('JSON');
     }
 }
Пример #6
0
 /**
  * Denied access
  * @param ForbiddenException $exception
  * @return \Bluz\Controller\Controller|null
  */
 public function forbidden(ForbiddenException $exception)
 {
     if (AuthProxy::getIdentity()) {
         $message = Translator::translate("You don't have permissions to access this page");
     } else {
         $message = Translator::translate("You don't have permissions, please sign in");
     }
     // for AJAX and API calls (over JSON)
     $jsonOrApi = Request::isXmlHttpRequest() || Request::getAccept([Request::TYPE_HTML, Request::TYPE_JSON]) == Request::TYPE_JSON;
     // for guest, for requests
     if (!AuthProxy::getIdentity() && !$jsonOrApi) {
         // save URL to session and redirect make sense if presentation is null
         Session::set('rollback', Request::getUri()->__toString());
         // add error notice
         Messages::addError($message);
         // redirect to Sign In page
         $url = Router::getUrl('users', 'signin');
         return $this->redirect($url);
     } else {
         return $this->error(new ForbiddenException($message, 403, $exception));
     }
 }