- How do I install diglactic/laravel-breadcrumbs in a Laravel project?
- Run `composer require diglactic/laravel-breadcrumbs` to install the package. Publish the configuration and views with `php artisan vendor:publish --provider="Diglactic\Breadcrumbs\BreadcrumbsServiceProvider"` to customize templates and settings. No migrations or database changes are required.
- What Laravel versions does this package support?
- The package supports Laravel 8 through 13, with versioned releases ensuring compatibility. Check the [compatibility chart](https://github.com/diglactic/laravel-breadcrumbs#compatibility-chart) for specific version mappings. Older versions (Laravel 7 and below) require the original package.
- How do I define breadcrumbs for a route in Laravel?
- Use the `Breadcrumbs::for()` method in your route definitions (e.g., `routes/web.php`). Define breadcrumbs in a closure, passing route names, URLs, or dynamic data like Eloquent models. Example: `Breadcrumbs::for('home', function ($breadcrumbs) { $breadcrumbs->push('Home', route('home')); });`
- Can I customize the HTML output of breadcrumbs?
- Yes, the package supports custom templates. Publish the default views with `php artisan vendor:publish --tag=views` and modify the Blade templates in `resources/views/vendor/breadcrumbs`. Pre-built templates for Bootstrap, Bulma, and Tailwind are included for quick integration.
- Does this package support structured data for SEO?
- Yes, the package includes built-in support for JSON-LD structured data. Enable it in the config file (`config/breadcrumbs.php`) and ensure your templates include the `{{ Breadcrumbs::render('jsonld') }}` directive. This improves search engine understanding of your breadcrumb hierarchy.
- How do I handle dynamic breadcrumbs (e.g., blog posts with categories)?
- Use closures with route parameters. For example, `Breadcrumbs::for('post', function ($breadcrumbs, $post) { $breadcrumbs->push($post->category->name, route('category', $post->category)); $breadcrumbs->push($post->title, route('post', $post)); });` This dynamically generates breadcrumbs based on the passed model or data.
- What happens if a breadcrumb route is undefined or 404s?
- By default, the package silently skips undefined routes. To handle errors, configure the `onUndefinedRoute` option in `config/breadcrumbs.php` to throw exceptions or redirect. For production, consider logging undefined routes to catch issues early.
- Can I cache breadcrumbs for better performance?
- The package generates breadcrumbs on-demand, but you can cache the rendered output manually. Store the result of `Breadcrumbs::render()` in Laravel’s cache system (e.g., `Cache::remember()`) for high-traffic pages. Avoid caching dynamic breadcrumbs that rely on real-time data.
- How do I test breadcrumb logic in Laravel?
- Test breadcrumb definitions by asserting the rendered output in PHPUnit. Use `Breadcrumbs::render()` in your tests and compare it to expected HTML. For dynamic breadcrumbs, mock route parameters or Eloquent models to simulate real-world scenarios. Example: `$this->assertStringContainsString('Home', Breadcrumbs::render());`
- Are there alternatives to this package for Laravel breadcrumbs?
- Yes, alternatives include `spatie/laravel-breadcrumbs` (more feature-rich but heavier) and `way/generators` (for dynamic breadcrumbs). This package prioritizes simplicity and Laravel-native design, making it ideal for projects needing lightweight, maintainable breadcrumbs without complex dependencies.