Installation
Add the bundle to your composer.json:
composer require art/breadcrumbs-bundle
Enable it in app/AppKernel.php:
new Art\BreadcrumbsBundle\ArtBreadcrumbsBundle(),
Configuration
Define breadcrumbs in Resources/config/breadcrumbs.yml:
breadcrumbs:
home:
uri: /
name: Home
products:
uri: /products
name: Products
parent: home
First Use Case Render breadcrumbs in a Twig template:
{{ render(controller('ArtBreadcrumbsBundle:Breadcrumbs:default')) }}
Or fetch them programmatically:
$breadcrumbs = $this->get('art_breadcrumbs')->getBreadcrumbs();
breadcrumbs.yml with parent keys.
admin:
uri: /admin
name: Admin
parent: home
product:
uri: /products/{id}
name: Product {name}
parent: products
Pass values via Twig:
{% set breadcrumbs = render(controller('ArtBreadcrumbsBundle:Breadcrumbs:default', {'id': product.id, 'name': product.name})) %}
Resources/views/Breadcrumbs/default.html.twig) to customize rendering.art_breadcrumbs service to fetch specific paths:
{% for crumb in art_breadcrumbs.getBreadcrumbs() %}
{{ crumb.name }} {% if not loop.last %} > {% endif %}
{% endfor %}
Art\BreadcrumbsBundle\Service\BreadcrumbsService to manipulate breadcrumbs dynamically:
$breadcrumbs = $this->get('art_breadcrumbs');
$breadcrumbs->add('custom', ['uri' => '/custom', 'name' => 'Custom']);
kernel.request) to modify breadcrumbs based on route parameters:
$event->getRequest()->attributes->set('breadcrumbs', ['custom' => ['uri' => '/custom', 'name' => 'Custom']]);
symfony/routing):
$router = $this->get('router');
$route = $router->getRouteCollection()->get('product_show');
$params = $route->getRequirements();
breadcrumbs.yml (YAML is strict about spacing).A is a parent of B, and B is a parent of A—this causes infinite loops.{param} syntax in uri and name fields, but validate that all placeholders are passed during rendering.breadcrumbs.yml file exists and is valid.AppKernel.php.uri paths in YAML match actual routes (e.g., /products/{id} vs. /products/show/{id}).BreadcrumbsService to fetch breadcrumbs from a database or API:
class CustomBreadcrumbsService extends \Art\BreadcrumbsBundle\Service\BreadcrumbsService {
public function getBreadcrumbs() {
// Custom logic here
}
}
Register it as a service in services.yml:
services:
art_breadcrumbs:
class: AppBundle\Service\CustomBreadcrumbsService
arguments: [...]
art_breadcrumbs.build events to modify the breadcrumb tree dynamically:
$dispatcher->addListener('art_breadcrumbs.build', function($event) {
$event->setBreadcrumbs(array_merge($event->getBreadcrumbs(), ['dynamic' => [...]]));
});
$cache = $this->get('cache');
$breadcrumbs = $cache->get('breadcrumbs', function() {
return $this->get('art_breadcrumbs')->getBreadcrumbs();
});
WhiteOctober/BreadcrumbsBundle or custom solutions.How can I help you explore Laravel packages today?