Weave Code
Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message
Twig Constant Accessor Bundle

Twig Constant Accessor Bundle Laravel Package

jdecool/twig-constant-accessor-bundle

Symfony bundle that integrates a Twig extension for reading PHP class constants and enum cases directly in templates. It exposes a dedicated accessor function so shared values can stay in PHP while remaining easy to use in Twig, without writing custom extensions.

View on GitHub
Deep Wiki
Context7

jdecool/twig-constant-accessor-bundle is a Symfony bundle that adds a constant() Twig function, letting templates read PHP class and global constants without hardcoding their values. It's handy when values like status codes, flags, or configuration keys are already defined in PHP and should stay in sync with your templates.

  • Access global constants (constant('PHP_EOL'))
  • Access class constants (constant('App\\Entity\\User::ROLE_ADMIN'))
  • Keeps template output aligned with PHP definitions
  • Simple Symfony bundle registration and autoconfiguration
Frequently asked questions about Twig Constant Accessor Bundle
Is jdecool/twig-constant-accessor-bundle a Laravel package?
No. It is a Symfony bundle that ships a Bundle subclass, a dependency-injection extension, and a compiler pass, and it is configured through YAML files under config/packages. Laravel has no bundle concept, compiler passes, or YAML package configuration, so the registration mechanism has no direct counterpart. Twig is also not the default template engine in Laravel, meaning you would need a separate Twig bridge before this package could even be relevant.
How do I install and configure it in a Laravel application?
You cannot install it with Composer and expect it to work unchanged, because its auto-registration relies on Symfony conventions. In a Laravel app you would copy the single Twig extension class into your own codebase and register it from AppServiceProvider or from your Twig bridge configuration. The bundle's only real deliverable is that extension plus an alias configuration array, so re-hosting it as a service provider is straightforward but is code you now own.
What does this bundle give me that Twig's native constant() function does not?
Twig's built-in constant() function has covered the core use case of reading class and global constants for years. This bundle only adds ergonomics around aliasing so template authors can avoid typing fully qualified class names. If shortening those names is not a stated requirement from your content or design teams, the native function or a simple view composer is enough.
Does it support Laravel 10, 11, or 12?
There is no official Laravel support. The repository is archived, has 9 stars and 0 dependents, and its last release was on 2024-05-02. An archived Symfony bundle from mid-2024 will likely pin Symfony 5 or 6 era constraints that can conflict with the Symfony components Laravel 11 pulls in transitively, so any fork would need those constraints widened and retested.
What are the alternatives for using PHP constants in Blade or Twig templates?
In Blade, the cleanest approach is to pass constants from the controller or a view composer, or to expose them through config values, rather than hardcoding class names in templates. In Twig, use the native constant() function directly. On PHP 8.1 and later, backed enums plus Twig's enum support cover the common case where designers need a readable value in a template.
Is this package maintained and safe to use in production?
The repository is archived, has very low adoption, and has no security patch stream or forward compatibility work for future PHP and Twig majors. The blast radius is small because the code only reads constants and renders scalars, so there is no meaningful remote code execution or data exposure surface. The real risks are abandonment and supply-chain hygiene, which make it a poor dependency to introduce into a graph you audit.
What PHP, Symfony, and Twig versions does it require?
You should read the archived composer.json rather than assume. Expect Symfony 5 or 6 era constraints and a twig/twig range that may not match the versions your Laravel application or Twig bridge actually uses. If you fork it, widen or drop those constraints and test against your PHP 8.1+ runtime and your installed Twig major.
Can I use it with a Laravel Twig bridge such as rcsofttech/twigbridge?
Possibly, but the bridge is a separate, independently maintained dependency with its own health and compatibility questions. You would not get the bundle's automatic registration and would instead register the extension manually in the bridge configuration. Assess the bridge's maintenance status first, because that dependency carries more risk than the extension code itself.
How would I fork or test this for a Laravel project?
The MIT license permits copying the roughly one class of behavior into something like app/Twig/ConstantAccessorExtension.php and registering it from AppServiceProvider. Then write feature tests that render templates using class and global constants and assert the rendered scalars. If you prefer a vendored fork, pin it to a commit, namespace it under your own vendor prefix, and add a composer replace or patch entry so it cannot drift.
When should I skip this package entirely?
Skip it if your application uses Blade, because the package is then inapplicable without adding Twig first. Skip it if you are on PHP 8.1 or later and backed enums cover the values your templates need. Skip it as a dependency if alias shortening is only an assumed convenience rather than a real requirement, and treat it as a reference implementation at most.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
shitoudev/permissionmanager
johannesclimacus/laravel-tui
hocvt/seotools
vitalybaev/sypexgeo
amphibee/themosis-ignition
gito-me/fast-excel
frankyso/laravel-ipaymu
geolize-studio/laravel-chile
boomerangui/chat
jinas/dhivatar
store-doc/laravel-storage-driver
plus-it/laravel-translation-manager
diephp/perhaps
diephp/telegraph
osmaviation/spreadsheet
microsoft/fluentui-system-icons
mmccook/php-json-canonicalization-scheme
middlewares/firewall
qubiqx/montapacking
nyoncode/laravel-package-console