Skip to content

Instantly share code, notes, and snippets.

@dhrrgn
Created September 3, 2014 13:40
Show Gist options
  • Save dhrrgn/694338ccd560b32696d2 to your computer and use it in GitHub Desktop.
Save dhrrgn/694338ccd560b32696d2 to your computer and use it in GitHub Desktop.
Symfony Request Extension
<?php
namespace Core\Http;
use Symfony\Component\HttpFoundation\Request as SymfonyRequest;
use Symfony\Component\HttpFoundation\ParameterBag;
class Request extends SymfonyRequest
{
/**
* @var ParameterBag
*/
public $json;
/**
* Sets the parameters for this request.
*
* This method also re-initializes all properties.
*
* @param array $query The GET parameters
* @param array $request The POST parameters
* @param array $attributes The request attributes (parameters parsed from the PATH_INFO, ...)
* @param array $cookies The COOKIE parameters
* @param array $files The FILES parameters
* @param array $server The SERVER parameters
* @param string $content The raw body data
* @api
*/
public function initialize(array $query = array(), array $request = array(), array $attributes = array(), array $cookies = array(), array $files = array(), array $server = array(), $content = null)
{
parent::initialize($query, $request, $attributes, $cookies, $files, $server, $content);
$this->json = new ParameterBag($this->isJson() ? (array) json_decode($this->getContent(), true) : []);
}
/**
* Return array or single key from $_GET
* @param string $key
* @param null $default
* @return mixed
*/
public function query($key, $default = null)
{
return $this->query->get($key, $default);
}
/**
* Return array or single key from $_POST
* @param string $key
* @param null $default
* @return mixed
*/
public function post($key, $default = null)
{
return $this->request->get($key, $default);
}
/**
* Return array or single key from $_SERVER
* @param string $key
* @param null $default
* @return mixed
*/
public function server($key, $default = null)
{
return $this->server->get($key, $default);
}
/**
* Return array or single key from $_FILES
* @param string $key
* @param null $default
* @return mixed
*/
public function file($key, $default = null)
{
return $this->files->get($key, $default);
}
/**
* Return array or single key from $_COOKIE
* @param string $key
* @param null $default
* @return mixed
*/
public function cookie($key, $default = null)
{
return $this->cookies->get($key, $default);
}
/**
* Return array or single key from headers taken from $_SERVER
* @param string $key
* @param null $default
* @return mixed
*/
public function header($key, $default = null)
{
return $this->headers->get($key, $default);
}
/**
* Parse the JSON body and get a value from it.
* @param string $key
* @param mixed $default
* @return mixed
*/
public function json($key, $default = null)
{
return $this->json->get($key, $default);
}
/**
* Determine if the request is a JSON request.
* @return bool
*/
public function isJson()
{
return $this->getContentType() === 'json';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment