Installation:
composer require asprega/breadcrumb-bundle
Add to config/bundles.php (Symfony 4+):
return [
// ...
AndreaSprega\Bundle\BreadcrumbBundle\AndreaSpregaBreadcrumbBundle::class => ['all' => true],
];
First Use Case: In a controller, dynamically add breadcrumbs via code:
public function showAction($id)
{
$builder = $this->get('asprega.breadcrumb.builder');
$builder->addItem('Home', 'home');
$builder->addItem('Posts', 'posts_list');
$builder->addItem('Post #$id', 'post_show', ['id' => $id]);
}
Render in Twig:
{{ render(controller('AndreaSpregaBreadcrumbBundle:Breadcrumb:breadcrumb')) }}
@Breadcrumb for declarative configuration.asprega_breadcrumb() in templates.Controller-Based Breadcrumb Building:
$builder->addItem(
'Dynamic Label', // Supports `$var.path` syntax
'route_name', // Optional route for clickability
['param' => 'value'], // Dynamic params via `$var.path`
'translation_domain' // Optional translation domain
);
Annotation-Based (Recommended for CRUD):
/**
* @Breadcrumb({
* {"label" = "home", "route" = "home"},
* {"label" = "$entity.title", "route" = "entity_show", "params" = {"id" = "$entity.id"}}
* })
*/
public function showAction(Entity $entity) { ... }
Dynamic Route Parameters: Leverage the current request’s URL parameters to avoid hardcoding:
// For URL `/posts/123/comments/456`, omit params if they exist in the request.
$builder->addItem('Post', 'post_show'); // Uses `id=123` from URL.
@Breadcrumb to override class-level items.breadcrumb.html.twig (located in vendor/asprega/breadcrumb-bundle/Resources/views/) for UI changes.{% set breadcrumbItems = breadcrumbItems|merge([{'label': entity.title}]) %}
kernel.response events.translationDomain to isolate translation keys (e.g., admin domain).Annotation Parsing:
doctrine/annotations is missing or misconfigured.doctrine/annotations is installed and cached:
php bin/console cache:clear
Dynamic Variables:
$var.path syntax fails if $var is not passed to the template.return $this->render('template.html.twig', ['entity' => $entity]);
Route Parameter Conflicts:
Symfony 5+ Twig Namespacing:
config/packages/asprega_breadcrumb.yaml:
asprega_breadcrumb:
template: AppBundle::breadcrumb.html.twig
dump($this->get('asprega.breadcrumb.builder')->getItems());
php bin/console debug:container --tag="asprega.breadcrumb" to verify annotation parsing.Custom Item Types:
Extend the BreadcrumbItem class to add metadata (e.g., icons, classes):
class CustomBreadcrumbItem extends BreadcrumbItem {
public $icon;
}
Override the builder to handle the new type.
Translation Overrides:
Use translationDomain: false for non-translatable labels:
$builder->addItem('Static Text', null, [], false);
Template Inheritance:
Override breadcrumb.html.twig to support:
aria-label).>).symfony/translation-contracts is v2.x for compatibility.php bin/console cache:pool:clear cache_annotations
abstract class BaseController extends Controller {
protected function initBreadcrumb() {
$this->get('asprega.breadcrumb.builder')->addItem('Home', 'home');
}
}
$builder = $this->createMock(BreadcrumbBuilder::class);
$builder->method('getItems')->willReturn([...]);
$this->container->set('asprega.breadcrumb.builder', $builder);
How can I help you explore Laravel packages today?