Skip to content

PHP API

Silarhi\TurboBundle\TurboManager reads the Turbo request headers off the current request of a RequestStack.

public function __construct(RequestStack $requestStack)
Method Returns
hasTurboFrame(): bool Whether the request has a Turbo-Frame header.
getTurboFrameId(): ?string The Turbo-Frame header: the id of the requesting frame, or null.
followTurboFrameRedirect(): bool Whether a redirect should be escalated: a Turbo-Frame header and no Turbo-Frame-Follow-Redirect header.
isTurboRequest(): bool Whether Turbo sent the request: an X-Turbo-Request-Id header (Turbo 8), or an Accept header with text/vnd.turbo-stream.html (the form submissions of Turbo 7 and 8).
getTurboRequestId(): ?string The X-Turbo-Request-Id header (Turbo 8), or null: the request-id of a broadcast refresh stream.
acceptsTurboStream(): bool Whether the Accept header has text/vnd.turbo-stream.html: the request takes a Turbo Stream answer.

Reading Turbo-Frame (hasTurboFrame(), getTurboFrameId(), followTurboFrameRedirect()) flags the request for Vary: Turbo-Frame, and acceptsTurboStream() (or isTurboRequest()) for Vary: Accept: TurboFrameListener adds them to the response (HTTP headers).

Without a current request on the stack (a console command, a message handler, an email rendered outside a request), every method answers as for a plain request: false, or null for getTurboFrameId() and getTurboRequestId().

Inject it in a controller to branch on frame requests yourself:

public function show(Project $project, TurboManager $turbo): Response
{
if ('project-details' === $turbo->getTurboFrameId()) {
return $this->render('project/_details.html.twig', ['project' => $project]);
}
return $this->render('project/show.html.twig', ['project' => $project]);
}

Silarhi\TurboBundle\EventListener\TurboFrameListener implements EventSubscriberInterface only, so it can be added to any Symfony event dispatcher.

public function __construct(TurboManager $turboManager, bool $followDeleteRedirects = true)
Method Effect
getSubscribedEvents(): array (static) kernel.response → onKernelResponse, default priority.
onKernelResponse(ResponseEvent $event): void Replaces a redirect with a 204 + Turbo-Location, keeping its other headers and cookies (rules), or turns it into a 303 (rules). Adds Vary: Turbo-Frame and Vary: Accept to the responses that read those headers.

Silarhi\TurboBundle\EventListener\TurboStreamListener appends the streams of TurboStreamCollector to the Turbo Stream response of the main request, on kernel.response.

public function __construct(TurboStreamCollector $turboStreams)

Silarhi\TurboBundle\EventListener\TurboFrameMissingListener logs a warning, on kernel.response, when the HTML answer to a frame request has no <turbo-frame> of that id (Finding a missing frame). Without a logger it does nothing. SilarhiTurboBundle registers it when kernel.debug is on.

public function __construct(?LoggerInterface $logger)

Silarhi\TurboBundle\Twig\TurboExtension is a classic Twig AbstractExtension (not the #[AsTwigFilter] attribute), so it works on Symfony 6.4 and 7.0 to 7.2, where attribute-based Twig extensions are not auto-registered.

public function __construct(TurboManager $turboManager, string $baseTemplate = 'base-frame.html.twig', TurboStreamBuilder $turboStreamBuilder = new TurboStreamBuilder())
Method Effect
getFilters(): array The turbo_frame and turbo_stream filters.
getFunctions(): array The turbo_frame_request, turbo_frame_id, turbo_request_id and turbo_stream functions, and the page helpers.
isTurboFrameRequest(?string $frameId = null): bool Whether the request targets a Turbo Frame, any or the given one (functions).
turboFrameId(): ?string The id of the requesting frame, or null.
turboRequestId(): ?string The X-Turbo-Request-Id of the request, or null (request id).
turboFrame(Environment $environment, string $template, ?string $frameId = null, ?string $baseTemplate = null): string The template to extend (resolution).
turboStream(string|Stringable $content, string $action, ?string $target = null, ?string $targets = null, array $attributes = []): string The content wrapped in a <turbo-stream> (filter).
turboRefreshesWith(string $method = 'replace', string $scroll = 'reset'): string The turbo-refresh-method and turbo-refresh-scroll meta tags (page helpers).
turboExemptsPageFromCache(): string, turboExemptsPageFromPreview(): string, turboPageRequiresReload(): string The turbo-cache-control (no-cache, no-preview) and turbo-visit-control (reload) meta tags.

The Twig function uses turbo_stream(action, target = null, content = '', targets = null, attributes = {}). It escapes plain content and preserves captured Twig markup; the filter takes the content on its left.

Silarhi\TurboBundle\TurboStreamRenderer renders a template, or one of its blocks, as a Turbo Stream response (Answering with Turbo Streams). It depends on Twig only.

public function __construct(Environment $twig)
Method Returns
render(string $view, array $parameters = [], ?string $block = null, int $status = 200): Response The rendered template, or its $block, with the text/vnd.turbo-stream.html content type.

TurboManager::STREAM_CONTENT_TYPE holds that content type.

Silarhi\TurboBundle\TurboStreamCollector collects Turbo Streams on the main request of a RequestStack (Adding streams from anywhere). It depends on HttpFoundation only.

public function __construct(RequestStack $requestStack, TurboStreamBuilder $turboStreamBuilder)
Method Effect
append(), prepend(), before(), after() (string $target, string $html): static Adds a stream of that action.
replace(), update() (string $target, string $html, bool $morph = false): static Adds a stream of that action, with method="morph" when $morph.
remove(string $target): static Adds a remove stream.
refresh(?string $requestId = null): static Adds a refresh stream, with its request-id.
action(string $action, ?string $target = null, string $html = '', ?string $targets = null, array $attributes = []): static Adds a stream of any action.
add(string $streams): static Adds streams already rendered.
all(): array, hasStreams(): bool The streams collected, and whether there are any.
flush(): string The streams collected, which it forgets.
createResponse(int $status = 200): Response A Turbo Stream response with the streams collected, which it forgets.

Silarhi\TurboBundle\TurboStreamBuilder renders the markup of a single <turbo-stream>. It depends on nothing, and TurboStreamCollector and the turbo_stream Twig helpers get it injected.

Method Returns
build(string $action, ?string $target = null, string $content = '', ?string $targets = null, array $attributes = []): string The stream; $content is HTML, not escaped. Throws on a $target and $targets both.

Silarhi\TurboBundle\Test\TurboAssertionsTrait is a trait for PHPUnit tests (Testing). Every method is public static.

Method Effect
getTurboFrameHeaders(string $frameId): array The $server parameters of a frame request: Turbo-Frame and a new X-Turbo-Request-Id.
getTurboStreamHeaders(): array The $server parameters of a Turbo 8 form submission: the stream Accept and a new X-Turbo-Request-Id.
assertTurboStreamResponse(Response $response, string $message = ''): void Asserts a text/vnd.turbo-stream.html content type.
assertTurboStream(Response $response, string $action, ?string $target = null, string $message = ''): void Asserts a <turbo-stream> with this action, and this target or targets when given.
assertNoTurboStream(Response $response, ?string $action = null, ?string $target = null, string $message = ''): void Asserts no <turbo-stream>, or none with this action (and target).
assertTurboStreamCount(Response $response, int $expectedCount, string $message = ''): void Asserts this number of <turbo-stream> elements.
assertTurboStreamContains(Response $response, string $action, ?string $target, string $expected, string $message = ''): void Asserts a matching stream has $expected in the HTML or the text of its <template>.
assertTurboFrame(Response $response, string $frameId, string $message = ''): void Asserts a <turbo-frame> of this id.
assertTurboLocation(Response $response, string $expectedUrl, string $message = ''): void Asserts a 204 with this Turbo-Location.

Silarhi\TurboBundle\SilarhiTurboBundle is the optional bundle: it defines the configuration and registers TurboManager, TurboFrameListener, TurboStreamCollector and TurboStreamListener, TurboFrameMissingListener when kernel.debug is on, plus TurboExtension and TurboStreamRenderer when Twig is installed (registered services).