oskarstark/symfony-http-responder
Composer 安装命令:
composer require 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.
README 文档
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'); } }
oskarstark/symfony-http-responder 适用场景与选型建议
oskarstark/symfony-http-responder 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 829.49k 次下载、GitHub Stars 达 13, 最近一次更新时间为 2021 年 03 月 17 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「symfony」 「json」 「file」 「twig」 「response」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 oskarstark/symfony-http-responder 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 oskarstark/symfony-http-responder 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 oskarstark/symfony-http-responder 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Kinikit - PHP Application development framework MVC component
The file manager intended for using Laravel with CKEditor / TinyMCE / Colorbox
The bundle for easy using json-rpc api on your project
ext-json wrapper with sane defaults
A package to cast json fields, each sub-keys is castable
Bundle Symfony DaplosBundle
统计信息
- 总下载量: 829.49k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 13
- 点击次数: 26
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-03-17