Assigning layout in bulk using route group? (v2)

In v1.* of Livewire when using the Route::livewire() syntax, I could use a route group and use the fluent ->layout() directive to assign a layout to a group of Livewire components:

Route::layout('my.custom.layout')->middleware('auth')->group(function()
    Route::livewire(...); // v1 syntax
});

With the v2 overhaul eliminating Route::livewire(), we now have the ->extends() and ->layout() directives, but they go onto our component render methods instead.

The thing is… I still want to apply the same layout to all of my Livewire components… and now cannot use the same Route Group approach. I have experimented with trying to chain various things to Route::group(), but so far cannot get anything to work - I am stuck defining my layout and section on EVERY full-page Livewire component… which is a lot as I am trying to replace controllers in my app with this. It’s workable but doesn’t feel very DRY to me.

I checked docs and config files and a bit of source, and have come up empty.

Anyone else out there have ideas? Am I missing something here, or even in core Laravel, that might help me accomplish this?

1 Like

Dove a bit more into source. It appears the layouts.app preset is an opinionated value that lives in Component.php:

// line 54
return app('view')->file(__DIR__."/Macros/livewire-view-{$layoutType}.blade.php", [
    'view'          => $this->initialLayoutConfiguration['view'] ?? 'layouts.app',
    'params'        => $this->initialLayoutConfiguration['params'] ?? [],
    'slotOrSection' => $this->initialLayoutConfiguration['slotOrSection'] ?? [
        'extends' => 'content', 'component' => 'default',
    ][$layoutType],
    'manager' => $manager,
]);

Unless there is a way to tap into $this->initialLayoutConfiguration['view'] and inject my custom view… I see two ways around this:

  1. Put in a PR to have the default view be configurable via the livewire.php config class… or
  2. Probably easiest to just make a layouts.app view that extends my real layout class. It’s a workaround but probably path-of-least-resistance.

Still open to suggestions from others though.