Weave Code
Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Symfony Http Responder Laravel Package

oskarstark/symfony-http-responder

Lightweight Symfony bundle that streamlines building HTTP responses by wrapping common response patterns in a simple responder layer. Helps keep controllers thin and consistent when returning JSON, redirects, views, files, and other responses across your app.

View on GitHub
Deep Wiki
Context7

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.

CI

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');
    }
}
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
uni-method/symfony-lib
tacticmedia/scim-server-bundle
neika-ai/symfony-bundle
mattleads/agent-publishing-bundle
glavweb/framework-bundle
glavweb/extra-bundle
glavweb/api-bundle
glavweb/admin-bundle
adachsoft/api-version-bundle
elsayed85/laravel-api-exceptions
ouhssini/laravel-demo-mode
daworks/ncloud-outbound-mailer-driver
gp247/cms
ignibyte/aic-gates
nexa-merchant/blog
novasa/laravel-language-center
opencage/geocode
adolfoholzer/parameters
livenetworks/ln-starter
cgv/localized-resource