Skip to content

Autowire TwigAwareController's default locale from the locale parameter - #3815

Closed
Vondry wants to merge 1 commit into
bolt:6.1from
Vondry:fix/autowire-twig-aware-controller-default-locale
Closed

Vondry wants to merge 1 commit into
bolt:6.1from
Vondry:fix/autowire-twig-aware-controller-default-locale

Conversation

@Vondry

@Vondry Vondry commented Oct 5, 2026

Copy link
Copy Markdown
Contributor

Why

My extension's controller extends TwigAwareController to use renderTemplate(), so it can render templates from the theme. Installing the extension breaks the container:

Cannot autowire service "鈥LlmsTxtController": argument "$defaultLocale" of method "Bolt\Controller\TwigAwareController::setAutowire()" is type-hinted "string", you should configure its value explicitly.

The extension doesn't need $defaultLocale itself. It comes with extending TwigAwareController, whose #[Required] setAutowire() must be fully autowirable. The value only comes from the bind in the project's config/services.yaml. Extension classes are registered by the generated config/services_bolt.yaml, which has no binds. So any extension controller extending TwigAwareController fails the same way.

What

Add #[Autowire(param: 'locale')] to $defaultLocale. It's the same value the bind already gives, so nothing changes for existing projects.

Extensions' controllers that extend TwigAwareController are registered by the
generated config/services_bolt.yaml, which has no `$defaultLocale` bind (only
the project's config/services.yaml has it). Autowiring the #[Required]
setAutowire() therefore failed with "argument $defaultLocale ... is type-hinted
string, you should configure its value explicitly", so the container could not
be compiled.

Take the value from the `locale` parameter with #[Autowire], as the bind does.
Vondry added a commit to Vondry/bolt-llms-txt that referenced this pull request Oct 5, 2026
Replace the reference-extension scaffold with the shared parts of the
hand-written llms.txt implementations, so a site only provides a Twig
template and optional config (enabled, template, locale, max_age):

- /llms.txt route rendering the theme's template, falling back to a
  shipped generic one; route defaults can serve more files such as
  /llms-full.txt
- text/plain response, public for max_age seconds with an xxh128 ETag
  and 304, without a firewall entry; private for logged-in users and
  whenever a cookie is set
- rendering in a configurable locale, checked against the site's locales
- html_to_text, markdown_label and markdown_url Twig filters
- PHPUnit tests, and ECS, PHPStan, Rector and PHPUnit in CI on PHP 8.2-8.5

The controller overrides TwigAwareController::setAutowire() only to add
#[Autowire] to $defaultLocale, which extension classes can't get from the
project's binds. bolt/core#3815 fixes this in core; the override is marked
for removal once that is released.
@bobvandevijver

Copy link
Copy Markdown
Member

Using Autowire or any other container related attributes are actually considered to be bad practice for anything else than end-user projects. Closing this as a won't merge, if you need to extend this controller it is also the responsibility of your extension to register its controller correctly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants