Conversation
|
Hi @retlehs, thanks for contributing! This would add non-negligible overhead to the runtime performance. If we were to add support for view composers it would need to be under a configuration option. My concern is, with this additional runtime overhead, is Blaze still significantly faster than Blade? Are you able to provide real-world benchmarks from an app that uses view composers heavily, comparing Blade vs Blaze (with this PR applied)? |
|
Thanks for the quick reply. I added the config gate ( I ran:
Iterations: 200, Warmup: 30, Components per render: 360
Notes:
Great suggestion on defaulting composer support off. It preserves the zero-cost default path, while apps that rely on composers can opt in and still see a substantial speedup over Blade in this benchmark (~81% lower avg render time). |
|
@retlehs would you be able to share some details about the use case for view composers in your app? I would like to understand the needs before we implement these features as they were intentionally left out for the initial release. |
|
I'm one of the maintainers of https://github.com/roots/sage (a long-standing WordPress starter theme with 10k+ stars) and https://github.com/roots/acorn (2M+ installs) - we even have folks using Livewire within WordPress We use Blade, and View Composers are used to pass data to the Blade templates In practice:
|
|
Appreciate the references! Interesting use case, we'll definitely consider this. We would probably add this along with some of the other features that were originally left out like View::share, in which case we would design a unified API for enabling these, but I'll keep this open for now. Thanks for the insights! |
|
Makes sense to design these together. From implementing composers, Happy to contribute to the unified API design if that would help move things forward. |
|
Hi @retlehs, Thanks for your contribution. We are looking to add support for view composers in Blaze, however I don't feel comfortable shipping this just yet. Blaze has only been out for a week at this point and we are mainly focused on compatibility with other packages and overall reliability. Adding view composers requires a lot of considerations and I would prefer not rushing it as there are some sharp edges. For that reason I'm going to close this PR now but hopefully we will revisit this soon. |
Problem
View::composer()registrations were ignored for Blaze components because Blaze compiles anonymous components into PHP functions that bypass Laravel's normalViewfactory render path wherecomposing:events are dispatched.Solution
Composer support is now opt-in behind config:
config('blaze.view_composers')BLAZE_VIEW_COMPOSERS=truefalseWhen enabled, Blaze injects:
into compiled component functions (before prop extraction). This delegates to Laravel’s existing
$factory->callComposer()flow using a lightweightComponentView, so existingView::composer()registrations continue to work without API changes.When disabled (default), Blaze emits no composer call in compiled output.
Behavior details
BladeService::pathToComponentName().foo/index.blade.php->foo,foo/foo.blade.php->foo).@blaze(fold: true)renders static HTML at compile time).Why this approach
View::composer()registration and maintain Blaze-only registryBlaze::composer()APIView::composer()usageConfig-gating keeps compatibility while making overhead an explicit opt-in tradeoff.
Test plan
Updated coverage includes both enabled and disabled modes:
blaze.view_composers=truegetData()*) applies to Blaze componentsIlluminate\\View\\Viewworksnav/index.blade.php) resolves tonavbadge/badge.blade.php) resolves tobadgeThis PR was developed with the assistance of Codex and Claude