Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mvrhov/mvrhovviewbundle
https://github.com/mvrhov/mvrhovviewbundle
php7 symfony symfony-bundle view
Last synced: 5 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/mvrhov/mvrhovviewbundle
- Owner: mvrhov
- License: mit
- Created: 2017-11-26T13:28:24.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2017-12-27T08:49:19.000Z (almost 7 years ago)
- Last Synced: 2024-04-19T10:03:53.821Z (7 months ago)
- Topics: php7, symfony, symfony-bundle, view
- Language: PHP
- Size: 6.84 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
### View bundle for Symfony
This bundle adds two View classes that you can return from your controller's action `RouteView`, `TemplateView` and
an interface `ResponderInterface` which the class you are returning from the controller should implement.### Installation
1. Require View bundle as a dependency using Composer:
```
php composer.phar require mvrhov/view-bundle
```2. Add bundle to `app/AppKernel.php`
```php
public function registerBundles()
{
return array(
new \mvrhov\ViewBundle\mvrhovViewBundle();
// ...
);
}
```3. You are done.
### Examples
## RouteView
```php
use mvrhov\ViewBundle\View\RouteView;
use mvrhov\ViewBundle\View\ViewInterface;final class RouteAction
{
public function __invoke(): ViewInterface
{
$params = [
'param1' => 'view',
'param2' => 'bundle',
];return new RouteView('my_route', $params);
}
}
```## TemplateView
```php
use mvrhov\ViewBundle\View\TemplateView;
use mvrhov\ViewBundle\View\ViewInterface;final class TemplateAction
{
public function __invoke(): ViewInterface
{
$data = [
'foo' => 1,
'bar' => 'yep'
];return new TemplateView('@Bundle/template.html.twig', $data);
}
}
```## Responder
```php
use mvrhov\ViewBundle\View\ResponderInterface;
use mvrhov\ViewBundle\View\TemplateView;
use mvrhov\ViewBundle\View\RouteView;
use mvrhov\ViewBundle\View\ResponseView;final class InvoiceResponder implements ResponderInterface;
{
private $invoices;public function __construct(array $invoices)
{
$this->invoices = $invoices;
}public function getView(Request $request, int $requestType): ViewInterface
{
if ('application/json' !== $request->getContentType()) {
$total = count($this->invoices);
if (0 === $total) {
return new RouteView('list_invoices');
}
if (1 === $total) {
return new TemplateView('@Bundle/template_one.html.twig', $this->invoices);
}
if (5 > $total) {
return new TemplateView('@Bundle/template_a_lot.html.twig', $this->invoices);
}
} else {
return new ResponseView(new Response(json_serialize($this->invoices)));
}
}
}use mvrhov\ViewBundle\View\TemplateView;
use mvrhov\ViewBundle\View\ResponderInterface;final class ResponderAction
{
public function __invoke(): ResponderInterface
{
$invoices = $this->getInvoices();return new InvoiceResponder($invoices);
}
}
```