Installation:
composer require azri/symfony-breadcrumb
Enable the bundle in config/bundles.php:
Azri\BreadcrumbBundle\AzriBreadcrumbBundle::class => ['all' => true],
Basic Configuration:
Add minimal config in config/azri_breadcrumb.yaml:
azri_breadcrumb: ~
Define Breadcrumbs in Routes:
Use breadcrumb options in your route definitions (e.g., config/routes.yaml):
acme_home:
path: /
options:
breadcrumb:
label: Home
Render in Twig: Include breadcrumbs in your template:
{{ breadcrumbs() }}
Display a simple breadcrumb trail for a static page (e.g., /contact):
# config/routes.yaml
acme_contact:
path: /contact
options:
breadcrumb:
label: Contact
parent_route: acme_home
Render in Twig:
{# templates/contact.html.twig #}
{{ breadcrumbs() }}
Output: Home > Contact
acme_product_show:
path: /products/{id}
options:
breadcrumb:
label: 'Product: %%name%%'
parent_route: acme_products_list
%%placeholder%% syntax for Twig/translation placeholders.
Replace in controller:
$breadcrumbProvider->getBreadcrumbByRoute('acme_product_show')
->setLabelParams(['name' => $product->getName()]);
foreach ($product->getCategories() as $category) {
$crumb = new Breadcrumb(
'Category: %name%',
'acme_category_show',
['id' => $category->id],
['name' => $category->name]
);
$collection->addBreadcrumbBeforeCrumb($crumb, $productCrumb);
}
@AzriBreadcrumb/breadcrumbs.html.twig).templates/breadcrumbs_custom.html.twig).azri_breadcrumb.yaml:
azri_breadcrumb:
template: 'templates/breadcrumbs_custom.html.twig'
breadcrumbs: Array of BreadcrumbInterface objects with:
route, routeParameters (for path()).label, labelParameters (for trans()).azri_breadcrumb:
template: '@AzriBreadcrumb/breadcrumbs_bootstrap.html.twig'
php bin/console cache:warmup to pre-generate breadcrumb collections.path(breadcrumb.route, breadcrumb.routeParameters) in Twig to generate links.<a href="{{ path(breadcrumb.route, breadcrumb.routeParameters) }}">
{{ breadcrumb.label|trans(breadcrumb.labelParameters) }}
</a>
messages.en.yaml):
breadcrumb:
product: 'Product: %name%'
options:
breadcrumb:
label: 'breadcrumb.product'
BreadcrumbProviderInterface to modify breadcrumbs on-the-fly:
public function showAction(BreadcrumbProviderInterface $provider, Product $product) {
$provider->getBreadcrumbByRoute('acme_product_show')
->setLabelParams(['%name%' => $product->getName()]);
}
$collection->addBreadcrumbBeforeCrumb(
new Breadcrumb(
'Parent: %name%',
'api_parent_show',
['id' => $parent->getId()],
['name' => $parent->getName()]
),
$currentCrumb
);
%%) Escape% in route labels (e.g., Product: %name%) triggers Symfony’s parameter injection.%% in route config, then replace with % in Twig:
{{ breadcrumb.label|replace({'%%': '%'})|trans(breadcrumb.labelParameters) }}
parent_route: acme_child) breaks the tree.if (!$provider->hasBreadcrumb('acme_child')) {
throw new \RuntimeException('Parent breadcrumb not found');
}
php bin/console cache:clear && php bin/console cache:warmup
cache:pool:clear azri_breadcrumb.cache_pool for targeted clearing.breadcrumbs variable.custom_breadcrumbs) or extend the base template.$breadcrumbs = $provider->getBreadcrumbs();
dump($breadcrumbs->toArray());
parent_route values match existing route names (case-sensitive).php bin/console debug:router to list all routes.options:
breadcrumb:
label: 'Raw Label' # Test without translation
BreadcrumbInterface and BreadcrumbCollectionInterface for custom logic:
azri_breadcrumb:
model_class: App\Model\CustomBreadcrumb
collection_class: App\Model\CustomBreadcrumbCollection
azri_breadcrumb:
provider_service_id: app.custom_breadcrumb_provider
BreadcrumbProviderInterface:
class CustomProvider implements BreadcrumbProviderInterface { ... }
kernel.request to modify breadcrumbs dynamically:
public function onKernelRequest(GetResponseEvent $event) {
$breadcrumbs = $event->getRequest()->get('breadcrumbs', []);
// Modify or add breadcrumbs...
}
breadcrumb() function in services.yaml:
services:
app.twig.breadcrumb_extension:
class: App\Twig\BreadcrumbExtension
tags:
- { name: twig.extension }
options.breadcrumb.label.options={"breadcrumb": {"label": "..."}}.<option key="breadcrumb">...</option>.@AzriBreadcrumb/breadcrumbs.html.twig.@AzriBreadcrumb/breadcrumbs_bootstrap.html.twig.templates/AzriBreadcrumb/ to override without vendor changes.setRouteParams() in controllers.setLabelParams() but respect Twig/translation placeholders.php bin
How can I help you explore Laravel packages today?