Warning
Laravel Breadcrumbs is no longer maintained. Please see the README for more details.
Defining Breadcrumbs
Breadcrumbs will usually correspond to actions or types of page. For each breadcrumb you specify a name, the breadcrumb title and the URL to link it to. Since these are likely to change dynamically, you do this in a closure, and you pass any variables you need into the closure.
The following examples should make it clear:
Static pages
The most simple breadcrumb is probably going to be your homepage, which will look something like this:
Breadcrumbs::register('home', function($breadcrumbs) {
$breadcrumbs->push('Home', route('home'));
});
As you can see, you simply call $breadcrumbs->push($title, $url)
inside the closure.
For generating the URL, you can use any of the standard Laravel URL-generation methods, including:
url('path/to/route')
(URL::to()
)secure_url('path/to/route')
route('routename')
orroute('routename', 'param')
orroute('routename', ['param1', 'param2'])
(URL::route()
)action('controller@action')
(URL::action()
)- Or just pass a string URL (
'http://www.example.com/'
)
This example would be rendered like this:
Home
Parent links
This is another static page, but this has a parent link before it:
Breadcrumbs::register('blog', function($breadcrumbs) {
$breadcrumbs->parent('home');
$breadcrumbs->push('Blog', route('blog'));
});
It works by calling the closure for the home
breadcrumb defined above.
It would be rendered like this:
Home / Blog
Note that the default template does not create a link for the last breadcrumb (the one for the current page), even when a URL is specified. You can override this by creating your own template - see Custom Templates for more details.
Dynamic titles and links
This is a dynamically generated page pulled from the database:
Breadcrumbs::register('page', function($breadcrumbs, $page) {
$breadcrumbs->parent('blog');
$breadcrumbs->push($page->title, route('page', $page->id));
});
The $page
variable would simply be passed in from the view:
{!! Breadcrumbs::render('page', $page) !!}
It would be rendered like this:
Tip: You can pass multiple parameters if necessary.
Nested categories
Finally if you have nested categories or other special requirements, you can call $breadcrumbs->push()
multiple times:
Breadcrumbs::register('category', function($breadcrumbs, $category) {
$breadcrumbs->parent('blog');
foreach ($category->ancestors as $ancestor) {
$breadcrumbs->push($ancestor->title, route('category', $ancestor->id));
}
$breadcrumbs->push($category->title, route('category', $category->id));
});
Alternatively you could make a recursive function such as this:
Breadcrumbs::register('category', function($breadcrumbs, $category) {
if ($category->parent)
$breadcrumbs->parent('category', $category->parent);
else
$breadcrumbs->parent('blog');
$breadcrumbs->push($category->title, route('category', $category->slug));
});
Both would be rendered like this:
Home / Blog / Grandparent Category / Parent Category / Category Title