Example #1
0
 /**
  * @param  string               $name               Name of the route
  * @param  array                $params             Parameters for the link
  * @param  array|Traversable    $options            Options for the route
  * @param  bool                 $reuseMatchedParams Whether to reuse matched parameters
  * @return string Url                         For the link href attribute
  */
 public function __invoke($name = null, $params = [], $options = [], $reuseMatchedParams = false)
 {
     $referer = $this->request->getHeader('Referer');
     if ($referer) {
         $refererUrl = $referer->uri()->getPath();
         // referer url
         $refererHost = $referer->uri()->getHost();
         // referer host
         $host = $this->request->getUri()->getHost();
         // current host
         // only redirect to previous page if request comes from same host
         if ($refererUrl && $refererHost == $host) {
             return $refererUrl;
         }
     }
     // redirect to home if no referer or from another page
     return $this->view->url($name, $params, $options, $reuseMatchedParams);
 }
Example #2
0
 /**
  * Check request content-type header to require JSON for methods with payloads.
  *
  * @param Request $request
  * @throws Exception\UnsupportedMediaTypeException
  */
 protected function checkContentType(Request $request)
 {
     // Require application/json Content-Type for certain methods.
     $method = strtolower($request->getMethod());
     $contentType = $request->getHeader('content-type');
     if (in_array($method, ['post', 'put', 'patch']) && !$contentType->match(['application/json', 'multipart/form-data'])) {
         $contentType = $request->getHeader('Content-Type');
         $errorMessage = sprintf('Invalid Content-Type header. Expecting "application/json", got "%s".', $contentType ? $contentType->getMediaType() : 'none');
         throw new Exception\UnsupportedMediaTypeException($errorMessage);
     }
 }