<?php
namespace Illuminate\Http;
use ArrayAccess;
use Closure;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Illuminate\Support\Traits\Macroable;
use RuntimeException;
use Symfony\Component\HttpFoundation\ParameterBag;
use Symfony\Component\HttpFoundation\Request as SymfonyRequest;
/**
* @method array validate(array $rules, ...$params)
* @method array validateWithBag(string $errorBag, array $rules, ...$params)
* @method bool hasValidSignature(bool $absolute = true)
*/
class Request extends SymfonyRequest implements Arrayable, ArrayAccess
{
use Concerns\InteractsWithContentTypes,
Concerns\InteractsWithFlashData,
Concerns\InteractsWithInput,
Macroable;
/**
* The decoded JSON content for the request.
*
* @var \Symfony\Component\HttpFoundation\ParameterBag|null
*/
protected $json;
/**
* All of the converted files for the request.
*
* @var array
*/
protected $convertedFiles;
/**
* The user resolver callback.
*
* @var \Closure
*/
protected $userResolver;
/**
* The route resolver callback.
*
* @var \Closure
*/
protected $routeResolver;
/**
* Create a new Illuminate HTTP request from server variables.
*
* @return static
*/
public static function capture()
{
static::enableHttpMethodParameterOverride();
return static::createFromBase(SymfonyRequest::createFromGlobals());
}
/**
* Return the Request instance.
*
* @return $this
*/
public function instance()
{
return $this;
}
/**
* Get the request method.
*
* @return string
*/
public function method()
{
return $this->getMethod();
}
/**
* Get the root URL for the application.
*
* @return string
*/
public function root()
{
return rtrim($this->getSchemeAndHttpHost().$this->getBaseUrl(), '/');
}
/**
* Get the URL (no query string) for the request.
*
* @return string
*/
public function url()
{
return rtrim(preg_replace('/\?.*/', '', $this->getUri()), '/');
}
/**
* Get the full URL for the request.
*
* @return string
*/
public function fullUrl()
{
$query = $this->getQueryString();
$question = $this->getBaseUrl().$this->getPathInfo() === '/' ? '/?' : '?';
return $query ? $this->url().$question.$query : $this->url();
}
/**
* Get the full URL for the request with the added query string parameters.
*
* @param array $query
* @return string
*/
public function fullUrlWithQuery(array $query)
{
$question = $this->getBaseUrl().$this->getPathInfo() === '/' ? '/?' : '?';
return count($this->query()) > 0
? $this->url().$question.Arr::query(array_merge($this->query(), $query))
: $this->fullUrl().$question.Arr::query($query);
}
/**
* Get the full URL for the request without the given query string parameters.
*
* @param array|string $query
* @return string
*/
public function fullUrlWithoutQuery($keys)
{
$query = Arr::except($this->query(), $keys);
$question = $this->getBaseUrl().$this->getPathInfo() === '/' ? '/?' : '?';
return count($query) > 0
? $this->url().$question.Arr::query($query)
: $this->url();
}
/**
* Get the current path info for the request.
*
* @return string
*/
public function path()
{
$pattern = trim($this->getPathInfo(), '/');
return $pattern === '' ? '/' : $pattern;
}
/**
* Get the current decoded path info for the request.
*
* @return string
*/
public function decodedPath()
{
return rawurldecode($this->path());
}
/**
* Get a segment from the URI (1 based index).
*
* @param int $index
* @param string|null $default
* @return string|null
*/
public function segment($index, $default = null)
{
return Arr::get($this->segments(), $index - 1, $default);
}
/**
* Get all of the segments for the request path.
*
* @return array
*/
public function segments()
{
$segments = explode('/', $this->decodedPath());
return array_values(array_filter($segments, function ($value) {
return $value !== '';
}));
}
/**
* Determine if the current request URI matches a pattern.
*
* @param mixed ...$patterns
* @return bool
*/
public function is(...$patterns)
{
$path = $this->decodedPath();
foreach ($patterns as $pattern) {
if (Str::is($pattern, $path)) {
return true;
}
}
return false;
}
/**
* Determine if the route name matches a given pattern.
*
* @param mixed ...$patterns
* @return bool
*/
public function routeIs(...$patterns)
{
return $this->route() && $this->route()->named(...$patterns);
}
/**
* Determine if the current request URL and query string match a pattern.
*
* @param mixed ...$patterns
* @return bool
*/
public function fullUrlIs(...$patterns)
{
$url = $this->fullUrl();
foreach ($patterns as $pattern) {
if (Str::is($pattern, $url)) {
return true;
}
}
return false;
}
/**
* Determine if the request is the result of an AJAX call.
*
* @return bool
*/
public function ajax()
{
return $this->isXmlHttpRequest();
}
/**
* Determine if the request is the result of a PJAX call.
*
* @return bool
*/
public function pjax()
{
return $this->headers->get('X-PJAX') == true;
}
/**
* Determine if the request is the result of a prefetch call.
*
* @return bool
*/
public function prefetch()
{
return strcasecmp($this->server->get('HTTP_X_MOZ') ?? '', 'prefetch') === 0 ||
strcasecmp($this->headers->get('Purpose') ?? '', 'prefetch') === 0;
}
/**
* Determine if the request is over HTTPS.
*
* @return bool
*/
public function secure()
{
return $this->isSecure();
}
/**
* Get the client IP address.
*
* @return string|null
*/
public function ip()
{
return $this->getClientIp();
}
/**
* Get the client IP addresses.
*
* @return array
*/
public function ips()
{
return $this->getClientIps();
}
/**
* Get the client user agent.
*
* @return string|null
*/
public function userAgent()
{
return $this->headers->get('User-Agent');
}
/**
* Merge new input into the current request's input array.
*
* @param array $input
* @return $this
*/
public function merge(array $input)
{
$this->getInputSource()->add($input);
return $this;
}
/**
* Replace the input for the current request.
*
* @param array $input
* @return $this
*/
public function replace(array $input)
{
$this->getInputSource()->replace($input);
return $this;
}
/**
* This method belongs to Symfony HttpFoundation and is not usually needed when using Laravel.
*
* Instead, you may use the "input" method.
*
* @param string $key
* @param mixed $default
* @return mixed
*/
public function get(string $key, $default = null)
{
return parent::get($key, $default);
}
/**
* Get the JSON payload for the request.
*
* @param string|null $key
* @param mixed $default
* @return \Symfony\Component\HttpFoundation\ParameterBag|mixed
*/
public function json($key = null, $default = null)
{
if (! iss