Laravel Framework, PHP & MySQL, Tutorials

Build a Dropbox File Manager in Laravel - Part 2 Breadcrumbs

Series


It's good that any folder on Dropbox can be accessed but it would be even better if we had a dynamic breadcrumb to allow you to jump back to any folder in the path, Let's add this!

Open FileManagerController.php and go to the Index method.

Add this snippet:

$breadcrumb = '';
$last_segment = '';
$breadcrumb.= '<ol class="breadcrumb">';
foreach (request()->segments() as $segment) {
    $last_segment .= '/' . $segment;
    $breadcrumb.= '<li class="breadcrumb-item"><a href="'.$last_segment.'">' . ucfirst($segment) . '</a></li>';
}
$breadcrumb.= '</ol>';

Let's go though what this code does.

First create the variables $breadcrumb and $last_segment.
The $breadcrumb will hold an orderd list with a class of breadcrumb. This is a standard bootstrap class to take care of the styling.

Next loop over request()->segments() this will containing an array of all the URL parts seperated from the slashes.
Upon each loop add the precedding slash to the $segment and add that to the a link. the $last_segment will contain the url for each folder path.

Pass the $breadcrumb to the view:

 return view('filemanager.index', compact('results', 'breadcrumb'));

Then display where you want the breadcrumbs to display:

{!! $breadcrumb !!}

 

David Carr

David Carr

For the past 12 years, I’ve been developing applications for the web using mostly PHP. I do this for a living and love what I do as every day there is something new and exciting to learn.

In my spare time, the web development community is a big part of my life. Whether managing online programming groups and blogs or attending a conference, I find keeping involved helps me stay up to date. This is also my chance to give back to the community that helped me get started, a place I am proud to be apart of.

Besides programming I love spending time with friends and family and can often be found together going out catching the latest movie, staying in playing games on the sofa or planning a trip to someplace I’ve never been before.