php
61 lines · 8 steps
Building a breadcrumb component in Laravel
A Blade component turns a URL path into an ordered trail of labeled, linked breadcrumbs.
Explained by
highlit
1<?php
2
3namespace App\View\Components;
4
5use Illuminate\Support\Str;
6use Illuminate\View\Component;
7
8class Breadcrumbs extends Component
9{
10 public array $trail;
11
12 public function __construct(?string $path = null)
13 {
14 $path = trim($path ?? request()->path(), '/');
15
16 $this->trail = $this->build($path);
17 }
18
19 protected function build(string $path): array
20 {
21 $crumbs = [[
22 'label' => 'Home',
23 'url' => url('/'),
24 'current' => $path === '',
25 ]];
26
27 if ($path === '') {
28 return $crumbs;
29 }
30
31 $segments = explode('/', $path);
32 $accumulated = '';
33 $last = count($segments) - 1;
34
35 foreach ($segments as $index => $segment) {
36 $accumulated .= '/' . $segment;
37
38 $crumbs[] = [
39 'label' => $this->humanize($segment),
40 'url' => url($accumulated),
41 'current' => $index === $last,
42 ];
43 }
44
45 return $crumbs;
46 }
47
48 protected function humanize(string $segment): string
49 {
50 if (ctype_digit($segment)) {
51 return '#' . $segment;
52 }
53
54 return Str::of($segment)->replace(['-', '_'], ' ')->title();
55 }
56
57 public function render()
58 {
59 return view('components.breadcrumbs');
60 }
61}
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Deriving view data in the constructor keeps templates dumb and focused on markup.
- 2Accumulating path segments as you loop lets each crumb link to its own partial URL.
- 3Small helpers like humanize centralize display formatting so labels stay consistent.
Related explainers
php
<?php class NameParser {
Parsing a full name into components in PHP
string-parsing
arrays
normalization
Intermediate
8 steps
php
<?php namespace App\Services\Checkout;
Validating coupons with Laravel's Pipeline
pipeline
chain of responsibility
transactions
Intermediate
7 steps
php
<?php namespace App\Services;
How a password strength validator works in PHP
validation
regular-expressions
data-driven
Intermediate
8 steps
php
<?php namespace App\Services;
Building a cached daily leaderboard in Laravel
caching
aggregation
eager-loading
Intermediate
9 steps
php
<?php final class RotatingFileLogger {
How a rotating file logger works in PHP
logging
file-rotation
io
Intermediate
9 steps
python
from typing import Any, Sequence, Mapping def render_markdown_table(
Rendering an aligned Markdown table in Python
string formatting
data transformation
closures
Intermediate
8 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/building-a-breadcrumb-component-in-laravel-explained-php-9f5f/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.