statamic / cms

The core Laravel CMS Composer package
https://statamic.com
Other
4.05k stars 530 forks source link

Wildcard view composer breaks entry.edit in CP #5982

Closed stuartcusackie closed 10 months ago

stuartcusackie commented 2 years ago

Bug description

Getting this error when opening the entry editor on the control panel: Method Illuminate\Support\Collection::title does not exist. (View: ...\vendor\statamic\cms\resources\views\entries\edit.blade.php)

I was messing with View Composers and this seems to break it, while all other pages are fine: App\Providers\AppServiceProvider.php

View::composer('*', function ($view) {
  $view->with('breadcrumbs', \Statamic::tag('nav:breadcrumbs')->params(['select' => 'title|url|is_current|collection|taxonomy'])->fetch());
});

I can fix it by removing the wildcard: View::composer(['partials.*', 'components.*'], function ($view) {

Not sure if I caused the problem elsewhere but perhaps someone can reproduce it. I would have thought the wildcard wouldn't include Statamic routes but I've no idea how that works.

How to reproduce

Use a wildcard on Laravel's view composer method.

Logs

No response

Versions

Statamic 3.3.10 Solo Laravel 9.11.0 PHP 8.0.10 spatie/statamic-responsive-images 2.11.2

Installation

Existing Laravel app

Antlers Parser

No response

Additional details

No response

duncanmcclean commented 10 months ago

I've closed this issue because we don't consider it to be a bug. You shouldn't add a view composer to all views, that's going to break things, even for first-party Laravel packages.

jasonvarga commented 10 months ago

You could check if it's a CP request though.

View::composer('*', function ($view) {
  if (Statamic::isCpRoute()) {
    return; // Do nothing in the CP.
  }

  $view->with('...');
});