Skip to content

ParamConverter for SensioFrameworkExtraBundle that adds convenient HTTP headers handling

Notifications You must be signed in to change notification settings

eduard-sukharev/request-headers-param-converter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Request Headers ParamConverter for SensioFrameworkExtraBundle

This package contains simple yet convenient ParamConverter for injecting HTTP headers into controller actions.

Read about SensioFrameworkExtraBundle on its official homepage.

Usage

Define RequestHeader as a service (in config.yml):

services:
    EdSukharev\App\ParamConverter\RequestHeaders:
        tags:
            - { name: 'request.param_converter', converter: 'request_header_converter', priority: '-60' }

And in your Controller:

namespace App\Controller\Api;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Annotation\Route;

class ApiController
{
    /**
     * @Route("/", methods={"GET"})
     * @ParamConverter("XAuthenticatedUserId", class="int", isOptional=true, converter="request_header_converter")
     * @ParamConverter("ContentType", class="string", isOptional=false, converter="request_header_converter")
     * @ParamConverter("XRequireAuth", class="string", isOptional=false, converter="request_header_converter")
     */
    public function getUserInfo($xAuthenticatedUserId, $contentType, $xRequireAuth) {
        return new JsonResponse([
            'XAuthenticatedUserId' => $xAuthenticatedUserId,
            'ContentType' => $contentType,
            'XRequireAuth' => $xRequireAuth,
        ]);
    }
}

Then, provided following request:

curl localhost -H 'x-require-auth: false' -H 'content-type: application/json'

the response will be:

{
  "XAuthenticatedUserId": null,
  "ContentType": "application/json",
  "XRequireAuth": false
}

Annotation parameters

ParamConverter annotation receives following parameters as arguments: - name — name of the header to look for, also used to generate controller action argument name - class — defines type of the argument. By default, all headers received as strings. - isOptional — when header is missing, and this is set to false, then \EdSukharev\App\ParamConverter\MissingHeaderException will be thrown, otherwise argument value will be set to null. - converter — has to be request_header_converter for this request parameter to be handled by this library.

About

ParamConverter for SensioFrameworkExtraBundle that adds convenient HTTP headers handling

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages