d13r / laravel-breadcrumbs

Please see the Diglactic fork.
https://github.com/diglactic/laravel-breadcrumbs
2.34k stars 416 forks source link

Custom Templates - not receiving array called $breadcrumbs. #167

Closed fishmad closed 6 years ago

fishmad commented 6 years ago

Using Laravel 5.5 and 4.x

The view does not receive an array called $breadcrumbs.

Works fine using package default templates, but a soon as you change the config file to point to a custom template file, the $breadcrumbs variable does not carry through.

    // 'view' => 'breadcrumbs::bootstrap4',
    'view' => '_partials/coreui/breadcrumbs',

@if (count($breadcrumbs))
    <ol class="breadcrumb">
        @foreach ($breadcrumbs as $breadcrumb)
            @if ($breadcrumb->url && !$loop->last)
                <li class="breadcrumb-item"><a href="{{ $breadcrumb->url }}">{{ $breadcrumb->title }}</a></li>
            @else
                <li class="breadcrumb-item active">{{ $breadcrumb->title }}</li>
            @endif
        @endforeach
    </ol>
@endif

ErrorException thrown with message "Undefined variable: breadcrumbs in \resources\views\_partials\coreui\breadcrumbs.blade.php) (View: C:\laragon\www\laravel55\resources\views\_partials\coreui\breadcrumbs.blade.php) (View: C:\laragon\www\laravel55\resources\views\_partials\coreui\breadcrumbs.blade.php)" image

In my case, breadcrumbs custom template is being called before the @yield('content') - not sure if this would cause the issues.

d13r commented 6 years ago

It works fine for me with custom views. What's in your view and layout?

On 12 Feb 2018 13:15, "Lee" notifications@github.com wrote:

Using Laravel 5.5 and 4.x

The view does not receive an array called $breadcrumbs.

Works fine using package default templates, but a soon as you change the config file to point to a custom template file, the $breadcrumbs variable does not carry through.

// 'view' => 'breadcrumbs::bootstrap4',
'view' => '_partials/coreui/breadcrumbs',

https://github.com/davejamesmiller/laravel-breadcrumbs#custom-templates

@if (count($breadcrumbs))

@endif

ErrorException thrown with message "Undefined variable: breadcrumbs in \resources\views_partials\coreui\breadcrumbs.blade.php) (View: C:\laragon\www\laravel55\resources\views_partials\coreui\breadcrumbs.blade.php) (View: C:\laragon\www\laravel55\resources\views_partials\ coreui\breadcrumbs.blade.php)" [image: image] https://user-images.githubusercontent.com/12012101/36098362-c95bb010-1052-11e8-9824-2772ac65a99b.png

In my case, breadcrumbs custom template is being called before the @yield https://github.com/yield('content') - not sure if this would cause the issues.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/davejamesmiller/laravel-breadcrumbs/issues/167, or mute the thread https://github.com/notifications/unsubscribe-auth/AAOcSOKv6btZU1HgCDnHfr1vaLSuPA48ks5tUDlngaJpZM4SCJxr .

fishmad commented 6 years ago

I'm guessing something has not installed correctly or registered in the boot.

config/breadcrumbs.php vendor file is published and is default: 'view' => 'breadcrumbs::bootstrap4',

routes/web.php looks like this with named routes:

Route::get('/', 'HomeController@index')->name('/');
Route::get('home', 'HomeController@index')->name('home');
Route::view('about', 'about')->name('about');
Route::view('contact', 'contact')->name('contact');
Route::view('dashboard', 'dashboard')->name('dashboard');

routes/breadcrumbs.php looks like this:

// Home
Breadcrumbs::register('/', function ($breadcrumbs) {
  $breadcrumbs->push('Home', route('home'));
});
// Home
Breadcrumbs::register('home', function ($breadcrumbs) {
  $breadcrumbs->push('Home', route('home'));
});
// Dashboard
Breadcrumbs::register('dashboard', function ($breadcrumbs) {
  $breadcrumbs->parent('home');
  $breadcrumbs->push('Dashboard', route('dashboard'));
});
// Home > About
Breadcrumbs::register('about', function ($breadcrumbs) {
  $breadcrumbs->parent('home');
  $breadcrumbs->push('About', route('about'));
});
// Home > About
Breadcrumbs::register('about', function ($breadcrumbs) {
  $breadcrumbs->parent('home');
  $breadcrumbs->push('About', route('about'));
});
// Home > About > Contact
Breadcrumbs::register('contact', function ($breadcrumbs) {
  $breadcrumbs->parent('about');
  $breadcrumbs->push('Contact us', route('contact'));
});

I had to define a custom $variable to the breadcrumb class in pure PHP for this to work, otherwise $breadcrumbs is not defined, does not appear to register.

My layout view file master.blade.php has an @include file as below @include('_partials.coreui.breadcrumbs')

This is my /_partials/coreui/breadcrumbs.blade.php view

@php
$breadcrumbs = Breadcrumbs::generate()
@endphp

<!-- Breadcrumbs -->
@if(count($breadcrumbs))
    <ol class="breadcrumb">
        @foreach ($breadcrumbs as $breadcrumb)
          @if ($breadcrumb->url && !$loop->last)
            <li class="breadcrumb-item"><a href="{{ $breadcrumb->url }}">{{ $breadcrumb->title }}</a></li>
          @else
            <li class="breadcrumb-item active">{{ $breadcrumb->title }}</li>
          @endif
        @endforeach
        <li class="breadcrumb-menu d-md-down-none">
          <div class="btn-group" role="group" aria-label="Button group">
            <a class="btn" href="#"><i class="icon-speech"></i></a>
            <a class="btn" href="./"><i class="icon-graph"></i> &nbsp;Dashboard</a>
            <a class="btn" href="#"><i class="icon-settings"></i> &nbsp;Settings</a>
          </div>
        </li>
      </ol>
@endif
<!-- /.Breadcrumbs -->

It is working now, but I don't think this is the intended elegant way to achieve it. image

d13r commented 6 years ago

This line is wrong:

@include('_partials.coreui.breadcrumbs')

https://github.com/davejamesmiller/laravel-breadcrumbs#4-output-the-breadcrumbs

d13r commented 6 years ago

To change the view, change the config file not the layout.

https://github.com/davejamesmiller/laravel-breadcrumbs#3-choose-a-template