digitalrespawn/breadcrumb-bundle
Simple Symfony bundle for building and rendering breadcrumbs in your application. Provides an easy way to define breadcrumb items and generate breadcrumb trails for pages. Includes documentation in Resources/doc and is released under the MIT license.
Install via Composer:
composer require digitalrespawn/breadcrumb-bundle
Enable the Bundle in config/bundles.php:
return [
// ...
DigitalRespawn\BreadcrumbBundle\DigitalRespawnBreadcrumbBundle::class => ['all' => true],
];
Publish Configuration (optional):
php artisan vendor:publish --provider="DigitalRespawn\BreadcrumbBundle\DigitalRespawnBreadcrumbBundle" --tag="config"
This creates config/breadcrumb.php with default settings.
First Use Case: Generate a breadcrumb in a controller or Twig template:
// Controller
$this->get('breadcrumbs')->add('Home', $this->generateUrl('homepage'));
$this->get('breadcrumbs')->add('Products', $this->generateUrl('product_index'));
Or in Twig:
{{ breadcrumbs() }}
Dynamic Breadcrumbs:
Use the Breadcrumbs service in controllers to dynamically set breadcrumbs based on route parameters:
$productId = $request->get('id');
$this->get('breadcrumbs')
->add('Home', $this->generateUrl('homepage'))
->add('Products', $this->generateUrl('product_index'))
->add('Product #'.$productId, $this->generateUrl('product_show', ['id' => $productId]));
Route-Based Generation:
Leverage Symfony’s ParamConverter to auto-generate breadcrumbs from route annotations (if using Symfony 2.x-style annotations):
# services.yml
services:
app.breadcrumb.listener:
class: AppBundle\EventListener\BreadcrumbListener
tags:
- { name: kernel.event_listener, event: kernel.controller, method: onKernelController }
Display Breadcrumbs:
Use the breadcrumbs() Twig function to render the trail:
{% if breadcrumbs %}
<nav class="breadcrumb">
<ul>
{% for breadcrumb in breadcrumbs %}
<li>
{% if loop.first %}
<a href="{{ breadcrumb.url }}">{{ breadcrumb.title }}</a>
{% else %}
<span>{{ breadcrumb.title }}</span>
{% endif %}
</li>
{% endfor %}
</ul>
</nav>
{% endif %}
Customize Output: Override the default Twig function by extending the bundle’s template:
{% extends 'DigitalRespawnBreadcrumbBundle::breadcrumbs.html.twig' %}
{% block breadcrumb_item %}
<li class="breadcrumb-item">{{ breadcrumb.title }}</li>
{% endblock %}
Kernel Events:
Listen to kernel.controller to set breadcrumbs globally:
public function onKernelController(GetResponseForControllerEvent $event)
{
$controller = $event->getController();
if (is_array($controller)) {
$object = $controller[0];
if (method_exists($object, 'setBreadcrumbs')) {
$object->setBreadcrumbs($this->get('breadcrumbs'));
}
}
}
Route Matching:
Use the Breadcrumbs service in route matchers or middleware to pre-populate breadcrumbs:
$breadcrumbs = $this->get('breadcrumbs');
$breadcrumbs->add('Category', $this->generateUrl('category_show', ['slug' => $slug]));
Route Generation Failures:
generateUrl() throws exceptions (e.g., route not found), wrap calls in a try-catch:
try {
$url = $this->generateUrl('route_name', $params);
} catch (\Exception $e) {
$url = '#'; // Fallback or handle gracefully
}
routing.yml or routes.php.Duplicate Entries:
$this->get('breadcrumbs')->clear();
Twig Function Not Found:
breadcrumbs() fails in Twig, verify the bundle is enabled and the Twig extension is loaded. Check config/bundles.php and app/AppKernel.php.Inspect Breadcrumbs: Dump the breadcrumb trail in a controller to verify content:
dump($this->get('breadcrumbs')->get());
Check Configuration:
Validate config/breadcrumb.php settings, especially:
enable_errors: Set to true during development to catch issues early.default_separator: Ensure it matches your UI (e.g., > or /).ParamConverter Issues:
Symfony\Component\HttpKernel\Controller\Controller and annotations are parsed correctly. For Laravel (Symfony-based), this may require additional setup.Custom Providers: Extend the bundle by creating a custom breadcrumb provider:
class CustomBreadcrumbProvider implements BreadcrumbProviderInterface
{
public function getBreadcrumbs()
{
return [
new Breadcrumb('Custom', $this->generateUrl('custom_route')),
];
}
}
Register it in services.yml:
services:
app.custom_breadcrumb_provider:
class: AppBundle\Service\CustomBreadcrumbProvider
tags:
- { name: breadcrumb.provider }
Override Templates:
Copy vendor/digitalrespawn/breadcrumb-bundle/Resources/views/breadcrumbs.html.twig to Resources/views/DigitalRespawnBreadcrumbBundle/breadcrumbs.html.twig to customize rendering.
Laravel-Specific Workarounds:
AppServiceProvider:
public function register()
{
$this->app->singleton('breadcrumbs', function ($app) {
return new \DigitalRespawn\BreadcrumbBundle\Service\BreadcrumbsService(
$app['router'],
$app['twig'],
$app['config']['breadcrumb']
);
});
}
route('name')) instead of Symfony’s generateUrl().Avoid Deprecation Warnings:
way/generators (for route-based breadcrumbs).
How can I help you explore Laravel packages today?