Download the PHP package synergitech/laravel-breadcrumbs without Composer
On this page you can find all versions of the php package synergitech/laravel-breadcrumbs. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download synergitech/laravel-breadcrumbs
More information about synergitech/laravel-breadcrumbs
Files in synergitech/laravel-breadcrumbs
Package laravel-breadcrumbs
Short Description A simple Laravel-style way to create breadcrumbs.
License MIT
Homepage https://github.com/davejamesmiller/laravel-breadcrumbs
Informations about the package laravel-breadcrumbs
Laravel Breadcrumbs
A simple Laravel-style way to create breadcrumbs.
Table of Contents
- Compatibility Chart
- Getting Started
- Defining Breadcrumbs
- Custom Templates
- Outputting Breadcrumbs
- Structured Data
- Route-Bound Breadcrumbs
- Advanced Usage
- API Reference
- Changelog
- Technical Support
- Bug Reports
- Contributing
-
License
Compatibility Chart
Laravel Breadcrumbs | Laravel | PHP |
---|---|---|
5.x | 5.6 | 7.1+ |
4.x | 5.5 | 7.0+ |
3.x | 5.0 – 5.4 | 5.4+ |
2.x | 4.0 – 4.2 | 5.3+ |
Getting Started
Note: If you are using an older version, click it in the table above to see the documentation for that version.
1. Install Laravel Breadcrumbs
Run this at the command line:
This will both update composer.json
and install the package into the vendor/
directory.
2. Define your breadcrumbs
Create a file called routes/breadcrumbs.php
that looks like this:
See the Defining Breadcrumbs section for more details.
3. Choose a template
By default a Bootstrap-compatible ordered list will be rendered, so if you're using Bootstrap 4 you can skip this step.
First initialise the config file by running this command:
Then open config/breadcrumbs.php
and edit this line:
The possible values are:
breadcrumbs::bootstrap4
– Bootstrap 4breadcrumbs::bootstrap3
– Bootstrap 3breadcrumbs::bootstrap2
– Bootstrap 2breadcrumbs::bulma
– Bulmabreadcrumbs::foundation6
– Foundation 6breadcrumbs::materialize
– Materializebreadcrumbs::json-ld
– JSON-LD Structured Data (<script> tag, no visible output)- The path to a custom view: e.g.
partials.breadcrumbs
See the Custom Templates section for more details.
4. Output the breadcrumbs
Finally, call Breadcrumbs::render()
in the view template for each page, passing it the name of the breadcrumb to use and any additional parameters – for example:
See the Route-Bound Breadcrumbs for a way to link breadcrumb names to route names automatically.
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:
As you can see, you simply call $trail->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:
And results in this output:
Home
Parent links
This is another static page, but this has a parent link before it:
It works by calling the closure for the home
breadcrumb defined above.
It would be rendered like this:
And results in this output:
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:
The $post
variable would simply be passed in from the view:
It results in this output:
Blog / Post Title
Tip: You can pass multiple parameters if necessary.
Nested categories
Finally, if you have nested categories or other special requirements, you can call $trail->push()
multiple times:
Alternatively you could make a recursive function such as this:
Both would be rendered like this:
And result in this:
Parent Category / Category Title
Custom Templates
Create a view
To customise the HTML, create your own view file (e.g. resources/views/partials/breadcrumbs.blade.php
) like this:
(See the views/ directory for the built-in templates.)
View data
The view will receive an array called $breadcrumbs
.
Each breadcrumb is an object with the following keys:
title
– The breadcrumb titleurl
– The breadcrumb URL, ornull
if none was given- Plus additional keys for each item in
$data
(see Custom data)
Update the config
Then update your config file (config/breadcrumbs.php
) with the custom view name, e.g.:
Skipping the view
Alternatively you can skip the custom view and call Breadcrumbs::generate()
to get the breadcrumbs Collection directly:
Outputting Breadcrumbs
Call Breadcrumbs::render()
in the view template for each page, passing it the name of the breadcrumb to use and any additional parameters.
With Blade
In the page (e.g. resources/views/home.blade.php
):
Or with a parameter:
With Blade layouts and @section
In the page (e.g. resources/views/home.blade.php
):
In the layout (e.g. resources/views/app.blade.php
):
Pure PHP (without Blade)
In the page (e.g. resources/views/home.php
):
Or use the long-hand syntax if you prefer:
Structured Data
To render breadcrumbs as JSON-LD structured data (usually for SEO reasons), use Breadcrumbs::view()
to render the breadcrumbs::json-ld
template in addition to the normal one. For example:
(Note: If you use Laravel Page Speed you may need to disable the TrimUrls
middleware.)
To specify an image, add it to the $data
parameter in push()
:
(If you prefer to use Microdata or RDFa you will need to create a custom template.)
Route-Bound Breadcrumbs
In normal usage you must call Breadcrumbs::render($name, $params...)
to render the breadcrumbs on every page. If you prefer, you can name your breadcrumbs the same as your routes and avoid this duplication...
Name your routes
Make sure each of your routes has a name. For example (routes/web.php
):
For more details see Named Routes in the Laravel documentation.
Name your breadcrumbs to match
For each route, create a breadcrumb with the same name and parameters. For example (routes/breadcrumbs.php
):
To add breadcrumbs to a custom 404 Not Found page, use the name errors.404
:
Output breadcrumbs in your layout
Call Breadcrumbs::render()
with no parameters in your layout file (e.g. resources/views/app.blade.php
):
This will automatically output breadcrumbs corresponding to the current route. The same applies to Breadcrumbs::generate()
:
And to Breadcrumbs::view()
:
Route binding exceptions
It will throw an InvalidBreadcrumbException
if the breadcrumb doesn't exist, to remind you to create one. To prevent this, first initialise the config file, if you haven't already:
Then open config/breadcrumbs.php
and set this value:
Similarly to prevent it throwing an UnnamedRouteException
if the current route doesn't have a name set this value:
Route model binding
Laravel Breadcrumbs uses the same model binding as the controller. For example:
This makes your code less verbose and more efficient by only loading the post from the database once.
For more details see Route Model Binding in the Laravel documentation.
Resourceful controllers
Laravel automatically creates route names for resourceful controllers, e.g. photo.index
, which you can use when defining your breadcrumbs. For example:
For more details see Resource Controllers in the Laravel documentation.
Advanced Usage
Breadcrumbs with no URL
The second parameter to push()
is optional, so if you want a breadcrumb with no URL you can do so:
The $breadcrumb->url
value will be null
.
The default Twitter Bootstrap templates provided render this with a CSS class of "active", the same as the last breadcrumb, because otherwise they default to black text not grey which doesn't look right.
Custom data
The push()
method accepts an optional third parameter, $data
– an array of arbitrary data to be passed to the breadcrumb, which you can use in your custom template. For example, if you wanted each breadcrumb to have an icon, you could do:
The $data
array's entries will be merged into the breadcrumb as properties, so you would access the icon as $breadcrumb->icon
in your template, like this:
Do not use the keys title
or url
as they will be overwritten.
Before and after callbacks
You can register "before" and "after" callbacks to add breadcrumbs at the start/end of the trail. For example, to automatically add the current page number at the end:
Getting the current page breadcrumb
To get the last breadcrumb for the current page, use Breadcrumb::current()
. For example, you could use this to output the current page title:
To ignore a breadcrumb, add 'current' => false
to the $data
parameter in push()
. This can be useful to ignore pagination breadcrumbs:
For more advanced filtering, use Breadcrumbs::generate()
and Laravel's Collection class methods instead:
Switching views at runtime
You can use Breadcrumbs::view()
in place of Breadcrumbs::render()
to render a template other than the default one:
Or you can override the config setting to affect all future render()
calls:
Or you could call Breadcrumbs::generate()
to get the breadcrumbs Collection and load the view manually:
Overriding the "current" route
If you call Breadcrumbs::render()
or Breadcrumbs::generate()
with no parameters, it will use the current route name and parameters by default (as returned by Laravel's Route::current()
method).
You can override this by calling Breadcrumbs::setCurrentRoute($name, $param1, $param2...)
.
Checking if a breadcrumb exists
To check if a breadcrumb with a given name exists, call Breadcrumbs::exists('name')
, which returns a boolean.
Defining breadcrumbs in a different file
If you don't want to use routes/breadcrumbs.php
, you can change it in the config file. First initialise the config file, if you haven't already:
Then open config/breadcrumbs.php
and edit this line:
It can be an absolute path, as above, or an array:
So you can use glob()
to automatically find files using a wildcard:
Or return an empty array []
to disable loading.
Defining/using breadcrumbs in another package
If you are creating your own package, simply load your breadcrumbs file from your service provider's boot()
method:
Dependency injection
You can use dependency injection to access the BreadcrumbsManager
instance if you prefer, instead of using the Breadcrumbs::
facade:
Macros
The BreadcrumbsManager class is macroable, so you can add your own methods. For example:
Advanced customisations
For more advanced customisations you can subclass BreadcrumbsManager and/or BreadcrumbsGenerator, then update the config file with the new class name:
(Note: Anything that's not part of the public API (see below) may change between releases, so I suggest you write unit tests to ensure it doesn't break when upgrading.)
API Reference
Breadcrumbs
Facade
Method | Returns | Added in |
---|---|---|
Breadcrumbs::for(string $name, closure $callback) |
void | 5.1.0 |
Breadcrumbs::register(string $name, closure $callback) |
void | 1.0.0 |
Breadcrumbs::before(closure $callback) |
void | 4.0.0 |
Breadcrumbs::after(closure $callback) |
void | 4.0.0 |
Breadcrumbs::exists() |
boolean | 2.2.0 |
Breadcrumbs::exists(string $name) |
boolean | 2.2.0 |
Breadcrumbs::generate() |
Collection | 2.2.3 |
Breadcrumbs::generate(string $name) |
Collection | 1.0.0 |
Breadcrumbs::generate(string $name, mixed $param1, ...) |
Collection | 1.0.0 |
Breadcrumbs::render() |
string | 2.2.0 |
Breadcrumbs::render(string $name) |
string | 1.0.0 |
Breadcrumbs::render(string $name, mixed $param1, ...) |
string | 1.0.0 |
Breadcrumbs::view(string $view) |
string | 4.0.0 |
Breadcrumbs::view(string $view, string $name) |
string | 4.0.0 |
Breadcrumbs::view(string $view, string $name, mixed $param1, ...) |
string | 4.0.0 |
Breadcrumbs::setCurrentRoute(string $name) |
void | 2.2.0 |
Breadcrumbs::setCurrentRoute(string $name, mixed $param1, ...) |
void | 2.2.0 |
Breadcrumbs::clearCurrentRoute() |
void | 2.2.0 |
Defining breadcrumbs
Method | Returns | Added in |
---|---|---|
$trail->push(string $title) |
void | 1.0.0 |
$trail->push(string $title, string $url) |
void | 1.0.0 |
$trail->push(string $title, string $url, array $data) |
void | 2.3.0 |
$trail->parent(string $name) |
void | 1.0.0 |
$trail->parent(string $name, mixed $param1, ...) |
void | 1.0.0 |
In the view (template)
Variable | Type | Added in |
---|---|---|
$breadcrumb->title |
string | 1.0.0 |
$breadcrumb->url |
string / null | 1.0.0 |
$breadcrumb->custom_attribute_name |
mixed | 2.3.0 |
Configuration
Setting | Type | Added in |
---|---|---|
view |
string | 2.0.0 |
files |
string / array | 4.0.0 |
unnamed-route-exception |
boolean | 4.0.0 |
missing-route-bound-breadcrumb-exception |
boolean | 4.0.0 |
invalid-named-breadcrumb-exception |
boolean | 4.0.0 |
manager-class |
string | 4.2.0 |
generator-class |
string | 4.2.0 |
Changelog
Laravel Breadcrumbs uses Semantic Versioning.
v5.1.0 (Sat 5 May 2018)
- Add
Breadcrumbs::for($name, $callback)
as an alias forBreadcrumbs::register($name, $callback)
- Renamed
$breadcrumbs
to$trail
in documentation (this doesn't affect the code)
These changes were inspired by (read: taken directly from) Dwight Watson's Breadcrumbs package.
Upgrading from 5.0.0 to 5.1.0
No changes are required, but I recommend updating your routes/breadcrumbs.php
to match the new documentation:
- Replace
Breadcrumbs::register
withBreadcrumbs::for
- Replace
$breadcrumbs
with$trail
v5.0.0 (Sat 10 Feb 2018)
- Add Laravel 5.6 support, and drop support for Laravel 5.5
- Drop PHP 7.0 support (add
void
return type hint, and use[]
instead oflist()
) - Fix class names in PhpDoc for
Breadcrumbs
facade when using IDE Helper
Upgrading from 4.x to 5.x
- Upgrade to Laravel 5.6 (requires PHP 7.1.3+)
- If you are extending any classes, add
: void
return type hints where needed.
Older versions
- Changelog for v4.x
- Changelog for v3.x
-
Technical Support
Sorry, I don't offer technical support. I spent a lot of time building Laravel Breadcrumbs and writing documentation, and I give it to you for free. Please don't expect me to also solve your problems for you.
I strongly believe that spending time fixing your own problems is the best way to learn, but if you're really stuck, I suggest you try posting a question on Stack Overflow, Laravel.io Forum or Laracasts Forum.
Bug Reports
Please open an issue on GitHub. But remember this is free software, so I don't guarantee to fix any bugs – I will investigate if/when I have the time and motivation to do so. Don't be afraid to go into the Laravel Breadcrumbs code (vendor/davejamesmiller/laravel-breadcrumbs/src/
), use var_dump()
to see what's happening and fix your own problems (and open a pull request with the fix if appropriate).
Contributing
Bug fixes: Please fix it and open a pull request. Bonus points if you add a unit test to make sure it doesn't happen again!
New features: Only high value features with a clear use case and well-considered API will be accepted. They must be documented and include unit tests. I suggest you open an issue to discuss the idea first.
Documentation: If you think the documentation can be improved in any way, please do edit this file and make a pull request.
Developing inside your application
The easiest way to work on Laravel Breadcrumbs inside your existing application is to tell Composer to install from source (Git) using the --prefer-source
flag:
Then:
Using your fork in a project
To use your own fork in a project, update the composer.json
in your main project as follows:
Replace YOUR_USERNAME
with your GitHub username and YOUR_BRANCH
with the branch name (e.g. develop
). This tells Composer to use your repository instead of the default one.
Running the test application
Alternatively there is a test application inside the repo runs on Docker Compose (tested on Linux only):
Unit tests
To run the unit tests:
To check code coverage:
Then open test-coverage/index.html
to view the results. Be aware of the edge cases in PHPUnit that can make it not-quite-accurate.
API Docs
To generate API docs using phpDocumentor:
Note: This currently fails for BreadcrumbsManager
due to function for(...)
, even though it's valid PHP7 code.
Other useful Docker Compose scripts
License
Copyright © 2013-2018 Dave James Miller
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
All versions of laravel-breadcrumbs with dependencies
illuminate/support Version 5.6.*|5.7.*
illuminate/view Version 5.6.*|5.7.*