Create Web Breadcrumbs with Laravel Breadcrumbs

Tram Ho

Introduce

During the development of the web, we often focus on the difficult and large features first, then the “beard” parts like breadcrumb, favicon, … Breadcrumbs is one of the things we do not think until you need it. But creating the breadcrumb as you like and the right path is not always simple. Fortunately, the author davejamesmiller has brought to the Laravel community a very “useful” package: Laravel Breadcrumbs

Setting

Use the command through the composer, guys: composer require davejamesmiller/laravel-breadcrumbs:5.x

How to use?

Definition of Breadcrumbs

Suppose your website has navigation bar with elements:

  • Home
  • About
  • Blog
  • Blog> Category
  • Blog> Category> Post

We need to create breadcrumbs.php file in the routes folder with content like this:

Explain

From the above code, you can also understand that: We use the Breadcrumbs facade to define a breadcrumbs, assign the parent, assign the URL.

  • To define a breadcrumbs component:

In that push will assign the URL to breadcrumbs through the router or with the case

Note We have many ways to generate URLs, not necessarily using route()

  • url('path/to/route') ( URL::to() )
  • secure_url('path/to/route')
  • route('routename') or route('routename', 'param') or route('routename', ['param1', 'param2']) ( URL::route() )
  • action(' [email protected] ') ( URL::action() )
  • Or maybe set the hard URL ( 'http://www.example.com/' )

Render

To render out of view you just need to use {{ Breadcrumbs::render('tên_breadcrumbs_đã_định_nghĩa') }} inside the Blade For example, you want to render outside of the 'home' component view:

Out put:

Home

Or an ingredient that includes a father

Output:

Home / Blog

In case the parameter needs to pass: This is a dynamically generated page pulled from the database:

Output:

Home / Blog / Post Title

Some note

  1. By default the package will use the style of Bootstrap 4, we can change the style for breadcrumbs by: First run the command:

Then open the config/breadcrumbs.php file and edit the line:

  1. By default, the final element will not be assigned a link, you can customize it by adding a bit of logic outside the Blade
  • Create a blade file with the content:

Then change to the value of

Conclusion

Laravel Breadcrumbs is an extremely useful package, we can reduce a lot of logic outside the Blade by using it, above are some basic features, if you want to understand and customize stronger than you can refer here

Share the news now

Source : Viblo