Last active
November 27, 2016 14:17
-
-
Save sveneisenschmidt/1ac60facffbf0bea36cb1b25631d937c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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