라라벨에서 Breadcrumb 을 손쉽게
설치
composer require davejamesmiller/laravel-breadcrumbs
config/app.php 에 ServiceProvier 와 Facade alias 등록
'providers' => [ // ... 'DaveJamesMiller\Breadcrumbs\ServiceProvider', ], 'aliases' => [ // ... 'Breadcrumbs' => 'DaveJamesMiller\Breadcrumbs\Facade', ],
Breadcrumb 정의
app/Http/breadcrumbs.php
<?php // Home Breadcrumbs::register('home', function($breadcrumbs) { $breadcrumbs->push('Home', route('home')); }); // Home > About Breadcrumbs::register('about', function($breadcrumbs) { $breadcrumbs->parent('home'); $breadcrumbs->push('About', route('about')); }); // Home > Blog Breadcrumbs::register('blog', function($breadcrumbs) { $breadcrumbs->parent('home'); $breadcrumbs->push('Blog', route('blog')); }); // Home > Blog > [Category] Breadcrumbs::register('category', function($breadcrumbs, $category) { $breadcrumbs->parent('blog'); $breadcrumbs->push($category->title, route('category', $category->id)); }); // Home > Blog > [Category] > [Page] Breadcrumbs::register('page', function($breadcrumbs, $page) { $breadcrumbs->parent('category', $page->category); $breadcrumbs->push($page->title, route('page', $page->id)); });
push() 뒤에 들어가는 route 이름은 routes.php 에 named route 로 지정해야 함.
템플릿 정의
default bootstrap 3
php artisan vendor:publish
config/breadcrumbs.php
<?php return [ 'view' => 'breadcrumbs::bootstrap3', ];
BreadCrumb 출력
HTML 로 출력하므로 블레이드 사용시 encoding 하지 않는 {!! !!}를 사용해야 함.
{!! Breadcrumbs::render('home') !!} {!! Breadcrumbs::render('category', $category) !!}
템플릿 변경
템플릿 파일을 resources 폴더내에 작성(Ex: resources/views/_partials/breadcrumbs.blade.php)
@if ($breadcrumbs) @foreach ($breadcrumbs as $breadcrumb) @if (!$breadcrumb->last) <li><a href="{{{ $breadcrumb->url }}}">{{{ $breadcrumb->title }}}</a></li> @else <li class="active">{{{ $breadcrumb->title }}}</li> @endif @endforeach @endif
에러 처리
[2015-05-26 09:18:09] local.ERROR: exception 'InvalidArgumentException' with message 'Route [home] not defined.' in myproj\vendor\laravel\framework\src\Illuminate\Routing\UrlGenerator.php:273 Stack trace: