PHP code example of toppy / symfony-async-twig-bundle
1. Go to this page and download the library: Download toppy/symfony-async-twig-bundle 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/ */
toppy / symfony-async-twig-bundle example snippets
namespace App\ViewModel;
use Toppy\AsyncViewModel\AsyncViewModel;
use Toppy\AsyncViewModel\Context\RequestContext;
use Toppy\AsyncViewModel\Context\ViewContext;
final class ProductStockViewModel implements AsyncViewModel
{
public function __construct(
private readonly StockService $stockService,
) {}
public function resolve(ViewContext $viewContext, RequestContext $requestContext): mixed
{
$productId = $requestContext->get('product_id');
return $this->stockService->getStock($productId);
}
}
// AsyncViewModel implementations are auto-tagged
$container->registerForAutoconfiguration(AsyncViewModel::class)
->addTag('toppy.async_view_model');
// EarlyHintsProvider implementations are auto-tagged
$container->registerForAutoconfiguration(EarlyHintsProviderInterface::class)
->addTag('toppy.early_hints_provider');
namespace App\ViewModel;
use Toppy\AsyncViewModel\AsyncViewModel;
// This class is auto-tagged with 'toppy.async_view_model'
final class MyViewModel implements AsyncViewModel
{
// ...
}
use Toppy\AsyncViewModel\WithDependencies;
final class OrderTotalsViewModel implements AsyncViewModel, WithDependencies
{
public static function getDependencies(): array
{
return [
CartItemsViewModel::class,
ShippingCostViewModel::class,
];
}
// Dependencies are resolved first, then this view model
}
use Symfony\Component\HttpFoundation\StreamedResponse;
use Toppy\TwigStreaming\Twig\StreamingTemplateRendererInterface;
final class ProductController
{
public function __construct(
private readonly StreamingTemplateRendererInterface $renderer,
) {}
public function show(int $id): StreamedResponse
{
return new StreamedResponse(
$this->renderer->stream('product/show.html.twig', [
'product_id' => $id,
])
);
}
}
use Twig\Environment;
final class ProductController
{
public function show(int $id, Environment $twig): Response
{
return new Response(
$twig->render('product/show.html.twig', ['product_id' => $id])
);
}
}
// ViewContext contains user/session state
$viewContext = $contextFactory->createViewContext();
// - currency: from session or 'EUR'
// - locale: from request locale
// - isB2B: from session
// - isVatExempt: from session
// - customerGroup: from session
// RequestContext contains route parameters
$requestContext = $contextFactory->createRequestContext(['extra' => 'param']);
// - params: merged from _route_params + additional
// - requestId: unique identifier for this request
// Automatically reset between requests in worker mode
$contextResolver->reset();
use Toppy\AsyncViewModel\Cache\CacheableViewModel;
final class ExpensiveViewModel implements AsyncViewModel, CacheableViewModel
{
public function getCacheTtl(): int
{
return 300; // Fresh for 5 minutes
}
public function getStaleWhileRevalidate(): int
{
return 600; // Serve stale for 10 more minutes while revalidating
}
public function getCacheTags(RequestContext $context): array
{
return ['product:' . $context->get('product_id')];
}
}
use Toppy\TwigStreaming\EarlyHints\EarlyHintsProviderInterface;
final class CustomEarlyHintsProvider implements EarlyHintsProviderInterface
{
public function getHints(): array
{
return [
[
'rel' => 'preload',
'href' => '/assets/critical.css',
'attributes' => ['as' => 'style'],
],
];
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.