FOSRestBundle icon indicating copy to clipboard operation
FOSRestBundle copied to clipboard

Add Headers to response

Open nicraMarcin opened this issue 6 years ago • 1 comments

Hello How to add custom headers to response?

config:

fos_rest:
    body_listener: true
    format_listener:
        rules:
            - { path: '^/api', priorities: ['json'], fallback_format: json, prefer_extension: false }
            - { path: '^/user', priorities: ['text/html'], fallback_format: 'text/html', prefer_extension: false }
    param_fetcher_listener: true
    view:
        view_response_listener: 'force'
        formats:
            json: true

I have in controller

/**
 * @Rest\Get("/", name="index",)
 * @param UserRepository $userRepository
 * @return array
 */
public function index(UserRepository $userRepository): array
{
$users = $userRepository->findAll();
return $users;
}

Now I need to add custome header X-Total-Count as count($users). How to do it with FOSRestBundle?

nicraMarcin avatar Apr 25 '18 13:04 nicraMarcin

Hello,

You can create a view_handler

thibaut-algrin avatar May 13 '18 08:05 thibaut-algrin