oskarstark / symfony-http-responder
This library provides a Symfony responder class, which can be used to render a template, return json or a file and redirect to route/url.
Installs: 546 270
Dependents: 2
Suggesters: 0
Security: 0
Stars: 12
Watchers: 3
Forks: 4
Open Issues: 1
Requires
- php: >=8.2
- symfony/http-foundation: ^5.4 || ^6.3 || ^7.0
- symfony/routing: ^5.4 || ^6.3 || ^7.0
- symfony/serializer: ^5.4 || ^6.3 || ^7.0
- twig/twig: ^2.13 || ^3.0
Requires (Dev)
- ergebnis/php-cs-fixer-config: ^5.0
- phpstan/phpstan: ^0.12.82
- phpunit/phpunit: ^9.5.3
- symfony/psr-http-message-bridge: ^2.1
Suggests
- symfony/psr-http-message-bridge: PSR-15 Request Handler Interface.
README
This library provides a Symfony responder class, which can be used to render a template, return json or a file and redirect to route/url.
This library is designed to be used in controllers which does not extend from AbstractController.
Installation
composer require oskarstark/symfony-http-responder
Usage
Render a Template
<?php declare(strict_types=1); namespace App\Controller; use OskarStark\Symfony\Http\Responder; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Annotation\Route; #[Route('/index', name: 'app_index')] final class IndexController { public function __construct( private Responder $responder, ) { } public function __invoke(): Response { return $this->responder->render('index.html.twig'); } }
Return JSON
<?php declare(strict_types=1); namespace App\Controller; use OskarStark\Symfony\Http\Responder; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Annotation\Route; #[Route('/api', name: 'app_api')] final class ApiController { public function __construct( private Responder $responder, ) { } public function __invoke(): Response { $data = [ 'foo' => 42, ]; return $this->responder->json($data); } }
Return a File
<?php declare(strict_types=1); namespace App\Controller; use OskarStark\Symfony\Http\Responder; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Annotation\Route; #[Route('/download', name: 'app_download')] final class DownloadController { public function __construct( private Responder $responder, ) { } public function __invoke(): Response { // You can either provide a filepath $file = '/app/invoices/invoice.pdf'; // or an SplFileObject $file = new \SplFileObject('/app/invoices/invoice.pdf'); return $this->responder->file($file); } }
Redirect to Url
<?php declare(strict_types=1); namespace App\Controller; use OskarStark\Symfony\Http\Responder; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Annotation\Route; #[Route('/redirect', name: 'app_redirect')] final class RedirectController { public function __construct( private Responder $responder, ) { } public function __invoke(): Response { return $this->responder->redirect('http://google.com'); } }
Redirect to Route
<?php declare(strict_types=1); namespace App\Controller; use OskarStark\Symfony\Http\Responder; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Annotation\Route; #[Route('/redirect', name: 'app_redirect')] final class RedirectController { public function __construct( private Responder $responder, ) { } public function __invoke(): Response { return $this->responder->route('app_my_route'); } }
PSR-7 and PSR-15 support
Symfony can respond using PSR-7 Response Interface instances when we are using the symfony/psr-http-message-bridge package, as described in this blog post.
# config/packages/oskarstark_symfony_http_responder.yaml services: # ... OskarStark\Symfony\Http\Psr7Responder: null
Render a Template
<?php declare(strict_types=1); namespace App\Controller; use OskarStark\Symfony\Http\Psr7Responder; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Server\RequestHandlerInterface; use Symfony\Component\Routing\Annotation\Route; final class RedirectHandler implements RequestHandlerInterface { public function __construct( private Psr7Responder $responder, ) { } #[Route('/index', name: 'app_index')] public function handle(ServerRequestInterface $request): ResponseInterface { return $this->responder->render('index.html.twig'); } }
Return JSON
<?php declare(strict_types=1); namespace App\Controller; use OskarStark\Symfony\Http\Psr7Responder; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Server\RequestHandlerInterface; use Symfony\Component\Routing\Annotation\Route; final class RedirectHandler implements RequestHandlerInterface { public function __construct( private Psr7Responder $responder, ) { } #[Route('/api', name: 'app_api')] public function handle(ServerRequestInterface $request): ResponseInterface { return $this->responder->json($data); } }
Return a File
<?php declare(strict_types=1); namespace App\Controller; use OskarStark\Symfony\Http\Psr7Responder; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Server\RequestHandlerInterface; use Symfony\Component\Routing\Annotation\Route; final class RedirectHandler implements RequestHandlerInterface { public function __construct( private Psr7Responder $responder, ) { } #[Route('/download', name: 'app_download')] public function handle(ServerRequestInterface $request): ResponseInterface { // You can either provide a filepath $file = '/app/invoices/invoice.pdf'; // or an SplFileObject $file = new \SplFileObject('/app/invoices/invoice.pdf'); return $this->responder->file($file); } }
Redirect to Url
<?php declare(strict_types=1); namespace App\Controller; use OskarStark\Symfony\Http\Psr7Responder; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Server\RequestHandlerInterface; use Symfony\Component\Routing\Annotation\Route; final class RedirectHandler implements RequestHandlerInterface { public function __construct( private Psr7Responder $responder, ) { } #[Route('/redirect', name: 'app_redirect')] public function handle(ServerRequestInterface $request): ResponseInterface { return $this->responder->redirect('http://google.com'); } }
Redirect to Route
<?php declare(strict_types=1); namespace App\Controller; use OskarStark\Symfony\Http\Psr7Responder; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Server\RequestHandlerInterface; use Symfony\Component\Routing\Annotation\Route; final class RedirectHandler implements RequestHandlerInterface { public function __construct( private Psr7Responder $responder, ) { } #[Route('/redirect', name: 'app_redirect')] public function handle(ServerRequestInterface $request): ResponseInterface { return $this->responder->route('app_my_route'); } }