Download the PHP package erickcomp/laravel-breadcrumbs-attributes without Composer
On this page you can find all versions of the php package erickcomp/laravel-breadcrumbs-attributes. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download erickcomp/laravel-breadcrumbs-attributes
More information about erickcomp/laravel-breadcrumbs-attributes
Files in erickcomp/laravel-breadcrumbs-attributes
Package laravel-breadcrumbs-attributes
Short Description Provides breadcrumbs to controller methods using PHP Attributes
License MIT
Homepage https://github.com/erickcomp/laravel-breadcrumbs-attributes
Informations about the package laravel-breadcrumbs-attributes
Use PHP 8 attributes to create breadcrumbs for controllers actions in a Laravel app
This package provides PHP 8 Attribute classes to automatically create breadcrumbs for your controller actions. This package was inspired by Spatie's wonderful Laravel Routes Attributes. In fact, a big portion of the "controller methods discovery" was copied from Laravel Routes Attributes package (Thanks for that, Spatie fellows!).
How it works
It works by scanning the controllers in the directories that contains your controllers and putting into a "Breadcrumb basket" all the crumbs you put in your controller actions and making it available at your controllers and views through DI or the \ErickComp\BreadcrumbAttributes\Facades\BreadcrumbsTrail
facade.
Custom controllers directories
If, for some, reason, you keep your controllers in a different directory, fear not! All you have to do is publish the config file and write down the directories where your controllers lie.
Collision when defining breadcrumbs
If you define breadcrumbs for the same route twice, and exception will be thrown. This behavior can be changed through the config . This config accepts one of these values:
-
: It's the default behavior. An exception will be thrown when the breadcrumbs scanner finds the second definition of the breadcrumb;
-
: The first definition of the breadcrumb will be used and the following ones will be silently ignored. It's useful to prevent a definition from a base controller class to be overriden by children controller classes.
- : The last definition of the breadcrumb will be used and the previous ones following will be silently ignored. It's useful to create a fallback breadcrumb definition on a base controller class while allowing it to be overriden into a child controller class.
Installation
You can install the package via composer:
You can publish the config file with:
This is the contents of the published config file:
Here you can customize the directories that will be scanned at your will and customize the file(s) that can contain Breadcrumbs declarations that do not use attributes (are done like standard route declarations).
Usage
The package provides an attribute (\ErickComp\BreadcrumbAttributes\Attributes\Breadcrumb
) that can handle breadcrumbs labels.
It can even handle breadcrumbs that require information that are available to the controller method you want to create a breadcrumb trail.
The Breadcrumb attribute can hold the following info:
- label
- parent
- name
- auxCrumbBefore (a crumb that will be inserted before the current crumb)
- auxCrumbBefore (a crumb that will be inserted after the current crumb)
You did not see the url of the crumb, right? It's because the URL of the crumb is evaluated based on the controller method the attribute is attached. If you create a Breadcrumb attribute and try to use it on a trail without referencing it into a route, an Exception will be thrown.
Adding a breadcrumb with a simple string label
Supposing you registered the routes like this:
And the current route in your browser is "http://localhost/my-route"
These attributes will automatically create breadcrumbs like:
Creating complex breadcrumbs based on the Request data
You have all the arguments passed to the method to which the breadcrumb is attached at your disposal in order to generate the ideal label for your breadcrumb. It's done by using the same patterns used on route definitions (enclosing route/controller methods on curly braces) to acccess the route parameters on the breadcrumb label. Here's an example:
You have worked the routes definitions this way:
And you want the user-show
breadcrumb label to be like 'Showing user: "John Doe"'
You would need to define your breadcrumbs somewhat this way:
Controller with breadcrumbs attributes:
The generated breadcrumbs will be like:
Note that the label is an array with string. All the items of the array are concatenated to form the final crumb.
The route parameter resolution
The breadcrumbs links deal with the url params and generate the url's using them, so you (most probably) won't have to deal with url generation for your breadcrumbs.
Aux breadcrumbs
Aux breadcrumbs are used to create "logical" breadcrumbs. They can be used to express menus, for example. Let's say you have a menu like this:
Your breadcrumbs for the "user-show" could be expressed like this:
Home > Admin > Users > Showing user "John Doe"
In this case, the "Admin" crumb does not have a corresponding url, but it makes sense in the breadcrumbs path
In order to insert this "Admin" crumb, you could rewrite the above example like this:
And the "Admin" crumb will be added to the breadcrumbs trail.
You can pass an array to the other Aux breadcrumbs param as well:
Integration with other packages (Spatie's Laravel Route Attributes)
As I said, this package was inspired by by Spatie's wonderful Laravel Routes Attributes. In fact, I have designed this package to work alongside Spatie's package, so it has some built-in integrations with it:
- If you're using custom directories for the controllers and you have defined them in the
route-attributes
config file, you don't have to redefine it again in the breadcrumbs config, because it tries to read the one from spatie's route attributes; - If you are naming your routes (and I advise you to do so!) you can omit the "name" argument of the Breadcrumb attribute, as the Breadcrumb is going to use the one defined in the route attribute.
Example:
Blade integration
This packages also provides a blade component that renders the breadcrumbs from the current route. It should be used like this inside your blade template:
To define classes of list items, you can specify:
This style of breadcrumbs component was made from the also awesome package Tabuna Breadcrumbs. Before coding this package, my intent was to use Tabuna breadcrumbs, but we cannot use closures on PHP attributes. But I got some concepts from there (and the component code) to build my own package as well.
Caching
This package works by checking the controller directories on every request and gather all the breadcrumbs information into our breadcrumb basket. Then, when requested by the programer (through the Trail class, the Breadcrumbs facade or the by using the erickcomp-breadcrumbs blade component), the breadcrumb trail is build based on the previously collected breadcrumbs. But in production mode, the breadcrumbs should not change, so we could entirely skip this gathering of breadcrumbs step. For that caching management, the package provides 2 artisan commands:
and
Attributeless breadcrumbs
If, for some reason you need declare breadcrumbs without using controller attributes, you can leverage the facade. It has methods to "put" crumbs into basket. By default, you can define breadcrumbs this way on the file . It can be changed by publishing the config file and changing the "breadcrumbs_files" entry. In this file, you can define breadcrumbs like:
You can manually create this file or you can use the command
Version 1
The first version of this package used other attribute classes to access the route/controller parameters. It worked well, but was too verbose. Now, all the features provided by those classes were migrated to the new syntax, that's more aligned to the Laravel's syntax to express route parameters on the route declarations.
Credits
I have searched and tested several routes and breadcrumbs packages and I liked Spatie's Laravel Route Attributes and Tabuna Breadcrumbs the most. As I was unable to make them work together organically, I decided to create my own package based on both of their ideas (and pieces of code. Once again, thank you both for that) and create something totally functional using PHP 8 Attributes.
License
The MIT License (MIT). Please see License File for more information.