Wallogit.com
2017 © Pedro Peláez
A simple and powerfull way to create breadcrumbs in Laravel 5+
A simple solution for breadcrumbs in Laravel 5+, (*1)
Edit your .json file and add the following line to your "require", (*2)
"mjanssen/laravel-5-breadcrumbs": "dev-master", (*3)
After this run the composer update to update your framework and get the breadcrumb class loaded into your files.
The files will be placed in the vendor/mjanssen/laravel-5-breadcrumbs folder., (*4)
The class can now be used within the framework. Make sure you use it in controllers for example., (*5)
use mjanssen\BreadcrumbsBundle\Breadcrumbs;, (*6)
As my first breadcrumb bundle (https://github.com/mjanssen/Laravel-4-breadcrumb) needed some attention (automatic generating, updated config file etc...). This breadcrumb bundle is optimized for Laravel 5., (*7)
At first, the Breadcrumbs will be empty. To add breadcrumbs, use the addBreadcrumb() function, (*8)
// Three breadcrumbs will be stored
Breadcrumbs::addBreadcrumb('Home', '/');
Breadcrumbs::addBreadcrumb('Second crumb', '/second-page');
Breadcrumbs::addBreadcrumb('Third crumb', '/second-page/third-page');
To generate the breadcrumb HTML, you can use the generate() function., (*9)
// Will return the html for the three previous set breadcrumbs. Breadcrumbs::generate();
If you are receiving an error about "undefined index ...", please make sure you copied the config file to your /config directory (under /config/breadcrumbs.php)., (*10)
For an easy way of generating breadcrumbs, the bundle also provides an automatic() function. This function will retreive the segments from the current url, and builds the breadcrumbs around them. Breadcrumb::generate() is not needed, since the automatic() function will build the breadcrumbs, and returns the HTML for the breadcrumbs., (*11)
Breadcrumbs::automatic();, (*12)
To remove all the breadcrumbs you set, simply use Breadcrumbs::truncate(); to remove all the stored breadcrumbs., (*13)
To get the stored breadcrumbs in a structured array, call Breadcrumbs::getBreadcrumbs();. This will return an array which contains the information used to create the breadcrumbs., (*14)
If you are interested to make use of the breadcrumbs globally, you can use the view composer function to make this happen., (*15)
Create a view called breadcrumbs.blade.php, (*16)
Inside the AppServiceProvider create a function called composer(), and call it from the already existing boot() function.
Inside the composer() function you can set composers for views. We will compose the breadcrumbs view., (*17)
View::composer('components.breadcrumbs', function() {
$data = [
'global_breadcrumbs' => Breadcrumbs::automatic()
];
view()->share($data);
});
Inside the breadcrumbs.blade.php file, you are now able to use the {!! $global_breadcrumbs !!} var.
Include the view from a master.blade.php file (or whatever your layout file is called). This can be done with @include, (*18)
@include(components/breadcrumbs) // Assuming you placed the breadcrumbs.blade.php file in a components folder, (*19)
The breadcrumbs will now show up on every page where your layout is used., (*20)
To make the home segment in the automatic breadcrumbs link to an other page, (*21)
first segment -> Home (example.com)` => `first segment -> Home (example.com/admin), (*22)
You can change the base url with Breadcrumbs::setHomeUrlBase("admin") (for this example). Please note, this function only works for the automatic breadcrumb generation, since the addBreadcrumb method already expects full url's., (*23)
uppercaseFirst useSeparator bootstrapSeparator separator lastBreadcrumbClickable automaticFirstCrumb ulLiClass bootstrap except stripHtmlExtension
Additionally you can use a config file to edit some features, for example the separator. To do this, create a php file in the
/config folder called: breadcrumbs.php. The source files of this Repo, there will be an example config file which
includes all the available options + information about them., (*24)
To remove id's (used for products or persons e.d.), put "numbers*" in the except array. This way numbers are removed from the breadcrumbs., (*25)
Config file can be found at: https://github.com/mjanssen/Laravel-5-breadcrumbs-bundle/blob/master/config_file_breadcrumbs.php, (*26)