PHP code example of oskarstark / symfony-http-responder

1. Go to this page and download the library: Download oskarstark/symfony-http-responder library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

oskarstark / symfony-http-responder example snippets




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



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



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



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



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



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



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



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



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



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