Skip to content

Instantly share code, notes, and snippets.

@sveneisenschmidt
Last active November 27, 2016 14:17
Show Gist options
  • Save sveneisenschmidt/1ac60facffbf0bea36cb1b25631d937c to your computer and use it in GitHub Desktop.
Save sveneisenschmidt/1ac60facffbf0bea36cb1b25631d937c to your computer and use it in GitHub Desktop.
<?php
namespace Bundle\FrontendBundle\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
class EventsController extends Controller
{
/**
*
* @Route(
* "/events",
* name="events_index"
* )
* @ParamConverter(
* name="limit",
* converter="QueryString"
* )
* @ParamConverter(
* name="accessToken",
* options={"field"="X-Facebook-AccessToken"},
* converter="HeaderField"
* )
*/
public function indexAction(string $accessToken = null, int $limit = 100): Response
{
if (!$accessToken || empty($accessToken)) {
return $this->json(null, Response::HTTP_UNAUTHORIZED);
}
// ...
return $this->json($response);
}
}
<?php
namespace Bundle\FrontendBundle\Request\ParamConverter;
use \Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use \Sensio\Bundle\FrameworkExtraBundle\Request\ParamConverter\ParamConverterInterface;
use \Symfony\Component\HttpFoundation\Request;
class HeaderFieldConverter implements ParamConverterInterface
{
/**
* @param ParamConverter $configuration
*
* @return bool
*/
public function supports(ParamConverter $configuration)
{
return $configuration->getConverter() === 'HeaderField';
}
public function apply(Request $request, ParamConverter $configuration)
{
$options = $configuration->getOptions();
$target = $configuration->getName();
$source = isset($options['field']) ? $options['field'] : $target;
if (!$request->headers->has($source)) {
return false;
}
$value = $request->headers->get($source);
$request->attributes->set($target, $value);
}
}
<?php
namespace Bundle\FrontendBundle\Request\ParamConverter;
use \Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use \Sensio\Bundle\FrameworkExtraBundle\Request\ParamConverter\ParamConverterInterface;
use \Symfony\Component\HttpFoundation\Request;
class QueryStringConverter implements ParamConverterInterface
{
/**
* @param ParamConverter $configuration
*
* @return bool
*/
public function supports(ParamConverter $configuration)
{
return $configuration->getConverter() === 'QueryString';
}
public function apply(Request $request, ParamConverter $configuration)
{
$source = $configuration->getName();
$target = $source;
if (!$request->query->has($source)) {
return false;
}
$value = $request->query->get($source);
$request->attributes->set($target, $value);
}
}
services:
# Query parameter converter
request.paramconverter.querystring:
class: Bundle\FrontendBundle\Request\ParamConverter\QueryStringConverter
tags:
- { name: request.param_converter, converter: QueryString }
# Header field converter
request.paramconverter.header:
class: Bundle\FrontendBundle\Request\ParamConverter\HeaderFieldConverter
tags:
- { name: request.param_converter, converter: HeaderField }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment