laravel-6 support

This commit is contained in:
RafficMohammed
2023-01-08 01:17:22 +05:30
parent 1a5c16ae4b
commit 774eed8b0e
4962 changed files with 279380 additions and 297961 deletions

View File

@@ -1,24 +0,0 @@
# http://editorconfig.org/
root = true
# Defaults - Linux style, 4 spaces
[*]
charset = utf-8
end_of_line = lf
indent_size = 4
indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true
# Markdown uses trailing whitespace to indicate continuation lines
[*.{markdown,md}]
trim_trailing_whitespace = false
# Ruby standard is 2 spaces
[{*.rb,Gemfile,Rakefile,Vagrantfile}]
indent_size = 2
# Ansible standard is 2 spaces
[ansible/**/*.yml]
indent_size = 2

View File

@@ -1,14 +0,0 @@
# PHP packages
/vendor
composer.phar
composer.lock
# Compiled documentation (run `grunt docs` for HTML; `grunt pdfdocs` for PDF)
/docs-html/
/docs-pdf/
# Test coverage output
/test-coverage/
# Operating system files
.DS_Store

View File

@@ -1,895 +0,0 @@
Laravel Breadcrumbs
================================================================================
[![Latest Stable Version](https://poser.pugx.org/davejamesmiller/laravel-breadcrumbs/v/stable?format=flat-square)](https://packagist.org/packages/davejamesmiller/laravel-breadcrumbs)
[![Total Downloads](https://poser.pugx.org/davejamesmiller/laravel-breadcrumbs/downloads?format=flat-square)](https://packagist.org/packages/davejamesmiller/laravel-breadcrumbs)
[![Monthly Downloads](https://poser.pugx.org/davejamesmiller/laravel-breadcrumbs/d/monthly?format=flat-square)](https://packagist.org/packages/davejamesmiller/laravel-breadcrumbs)
[![Latest Unstable Version](https://poser.pugx.org/davejamesmiller/laravel-breadcrumbs/v/unstable?format=flat-square)](https://packagist.org/packages/davejamesmiller/laravel-breadcrumbs)
[![License](https://poser.pugx.org/davejamesmiller/laravel-breadcrumbs/license?format=flat-square)](https://packagist.org/packages/davejamesmiller/laravel-breadcrumbs)
A simple Laravel-style way to create breadcrumbs in [Laravel](https://laravel.com/).
Table of Contents
--------------------------------------------------------------------------------
- [Compatibility Chart](#compatibility-chart)
- [Getting Started](#getting-started)
- [Defining Breadcrumbs](#defining-breadcrumbs)
- [Custom Templates](#custom-templates)
- [Outputting Breadcrumbs](#outputting-breadcrumbs)
- [Route-Bound Breadcrumbs](#route-bound-breadcrumbs)
- [Advanced Usage](#advanced-usage)
- [API Reference](#api-reference)
- [Changelog](#changelog)
- [Technical Support](#technical-support)
- [Bug Reports](#bug-reports)
- [Contributing](#contributing)
- [License](#license)
Compatibility Chart
--------------------------------------------------------------------------------
| Laravel Breadcrumbs | Laravel | PHP |
|------------------------------------------------------------------------|-----------|------|
| 3.0.2+ | 5.0 5.4 | 5.4+ |
| 3.0.1 | 5.0 5.3 | 5.4+ |
| 3.0.0 | 5.0 5.2 | 5.4+ |
| [2.x](https://github.com/davejamesmiller/laravel-breadcrumbs/tree/2.x) | 4.0 4.2 | 5.3+ |
Getting Started
--------------------------------------------------------------------------------
### 1. Install Laravel Breadcrumbs
#### Install with Composer
Run this at the command line:
```bash
composer require davejamesmiller/laravel-breadcrumbs
```
This will both update `composer.json` and install the package into the `vendor/` directory.
#### Add to `config/app.php`
Add the service provider to `providers`:
```php
'providers' => [
// ...
DaveJamesMiller\Breadcrumbs\ServiceProvider::class,
],
```
And add the facade to `aliases`:
```php
'aliases' => [
// ...
'Breadcrumbs' => DaveJamesMiller\Breadcrumbs\Facade::class,
],
```
### 2. Define your breadcrumbs
Create a file called `routes/breadcrumbs.php` that looks like this:
```php
<?php
// Home
Breadcrumbs::register('home', function($breadcrumbs)
{
$breadcrumbs->push('Home', route('home'));
});
// Home > About
Breadcrumbs::register('about', function($breadcrumbs)
{
$breadcrumbs->parent('home');
$breadcrumbs->push('About', route('about'));
});
// Home > Blog
Breadcrumbs::register('blog', function($breadcrumbs)
{
$breadcrumbs->parent('home');
$breadcrumbs->push('Blog', route('blog'));
});
// Home > Blog > [Category]
Breadcrumbs::register('category', function($breadcrumbs, $category)
{
$breadcrumbs->parent('blog');
$breadcrumbs->push($category->title, route('category', $category->id));
});
// Home > Blog > [Category] > [Page]
Breadcrumbs::register('page', function($breadcrumbs, $page)
{
$breadcrumbs->parent('category', $page->category);
$breadcrumbs->push($page->title, route('page', $page->id));
});
```
See the [Defining Breadcrumbs](#defining-breadcrumbs) section for more details.
### 3. Choose a template
By default a [Bootstrap](http://getbootstrap.com/components/#breadcrumbs)-compatible ordered list will be rendered, so if you're using Bootstrap 3 you can skip this step.
First initialise the config file by running this command:
```bash
php artisan vendor:publish
```
Then open `config/breadcrumbs.php` and edit this line:
```php
'view' => 'breadcrumbs::bootstrap3',
```
The possible values are:
- [Bootstrap 3](http://getbootstrap.com/components/#breadcrumbs): `breadcrumbs::bootstrap3`
- [Bootstrap 2](http://getbootstrap.com/2.3.2/components.html#breadcrumbs): `breadcrumbs::bootstrap2`
- The path to a custom view: e.g. `_partials/breadcrumbs`
See the [Custom Templates](#custom-templates) section for more details.
### 4. Output the breadcrumbs
Finally, call `Breadcrumbs::render()` in the view template for each page, passing it the name of the breadcrumb to use and any additional parameters for example:
```html+php
{!! Breadcrumbs::render('home') !!}
{!! Breadcrumbs::render('category', $category) !!}
```
See the [Outputting Breadcrumbs](#outputting-breadcrumbs) section for other output options, and see [Route-Bound Breadcrumbs](#route-bound-breadcrumbs) for a way to link breadcrumb names to route names automatically.
Defining Breadcrumbs
--------------------------------------------------------------------------------
Breadcrumbs will usually correspond to actions or types of page. For each breadcrumb you specify a name, the breadcrumb title and the URL to link it to. Since these are likely to change dynamically, you do this in a closure, and you pass any variables you need into the closure.
The following examples should make it clear:
### Static pages
The most simple breadcrumb is probably going to be your homepage, which will look something like this:
```php
Breadcrumbs::register('home', function($breadcrumbs) {
$breadcrumbs->push('Home', route('home'));
});
```
As you can see, you simply call `$breadcrumbs->push($title, $url)` inside the closure.
For generating the URL, you can use any of the standard Laravel URL-generation methods, including:
- `url('path/to/route')` (`URL::to()`)
- `secure_url('path/to/route')`
- `route('routename')` or `route('routename', 'param')` or `route('routename', ['param1', 'param2'])` (`URL::route()`)
- ``action('controller@action')`` (``URL::action()``)
- Or just pass a string URL (`'http://www.example.com/'`)
This example would be rendered like this:
> Home
### Parent links
This is another static page, but this has a parent link before it:
```php
Breadcrumbs::register('blog', function($breadcrumbs) {
$breadcrumbs->parent('home');
$breadcrumbs->push('Blog', route('blog'));
});
```
It works by calling the closure for the `home` breadcrumb defined above.
It would be rendered like this:
> [Home](#) / Blog
Note that the default template does not create a link for the last breadcrumb (the one for the current page), even when a URL is specified. You can override this by creating your own template see [Custom Templates](#custom-templates) for more details.
### Dynamic titles and links
This is a dynamically generated page pulled from the database:
```php
Breadcrumbs::register('page', function($breadcrumbs, $page) {
$breadcrumbs->parent('blog');
$breadcrumbs->push($page->title, route('page', $page->id));
});
```
The `$page` variable would simply be passed in from the view:
```html+php
{!! Breadcrumbs::render('page', $page) !!}
```
It would be rendered like this:
> [Home](#) / [Blog](#) / Page Title
**Tip:** You can pass multiple parameters if necessary.
### Nested categories
Finally if you have nested categories or other special requirements, you can call `$breadcrumbs->push()` multiple times:
```php
Breadcrumbs::register('category', function($breadcrumbs, $category) {
$breadcrumbs->parent('blog');
foreach ($category->ancestors as $ancestor) {
$breadcrumbs->push($ancestor->title, route('category', $ancestor->id));
}
$breadcrumbs->push($category->title, route('category', $category->id));
});
```
Alternatively you could make a recursive function such as this:
```php
Breadcrumbs::register('category', function($breadcrumbs, $category) {
if ($category->parent)
$breadcrumbs->parent('category', $category->parent);
else
$breadcrumbs->parent('blog');
$breadcrumbs->push($category->title, route('category', $category->slug));
});
```
Both would be rendered like this:
> [Home](#) / [Blog](#) / [Grandparent Category](#) / [Parent Category](#) / Category Title
Custom Templates
--------------------------------------------------------------------------------
### Create a view
To customise the HTML, create your own view file (e.g. `resources/views/_partials/breadcrumbs.blade.php`) like this:
```html+php
@if ($breadcrumbs)
<ul class="breadcrumb">
@foreach ($breadcrumbs as $breadcrumb)
@if (!$breadcrumb->last)
<li><a href="{{ $breadcrumb->url }}">{{ $breadcrumb->title }}</a></li>
@else
<li class="active">{{ $breadcrumb->title }}</li>
@endif
@endforeach
</ul>
@endif
```
(See the [views/ directory](https://github.com/davejamesmiller/laravel-breadcrumbs/tree/master/views) for the built-in templates.)
#### View data
The view will receive an array called `$breadcrumbs`.
Each breadcrumb is an object with the following keys:
- `title` The breadcrumb title (see :doc:`defining`)
- `url` The breadcrumb URL (see :doc:`defining`), or `null` if none was given
- `first` `true` for the first breadcrumb (top level), `false` otherwise
- `last` `true` for the last breadcrumb (current page), `false` otherwise
- Plus additional keys for each item in `$data` (see [Custom data](#custom-data))
### Update the config
Then update your config file (`config/breadcrumbs.php`) with the custom view name, e.g.:
```php
// resources/views/_partials/breadcrumbs.blade.php
'view' => '_partials/breadcrumbs',
```
Outputting Breadcrumbs
--------------------------------------------------------------------------------
Call `Breadcrumbs::render()` in the view template for each page, passing it the name of the breadcrumb to use and any additional parameters.
### With Blade
In the page (e.g. `resources/views/home.blade.php`):
```html+php
{!! Breadcrumbs::render('home') !!}
```
Or with a parameter:
```html+php
{!! Breadcrumbs::render('category', $category) !!}
```
### With Blade layouts and @section
In the page (e.g. `resources/views/home.blade.php`):
```html+php
@extends('layout.name')
@section('breadcrumbs', Breadcrumbs::render('home'))
```
In the layout (e.g. `resources/views/app.blade.php`):
```html+php
@yield('breadcrumbs')
```
### Pure PHP (without Blade)
In the page (e.g. `resources/views/home.php`):
```html+php
<?= Breadcrumbs::render('home') ?>
```
Or use the long-hand syntax if you prefer:
```html+php
<?php echo Breadcrumbs::render('home') ?>
```
Route-Bound Breadcrumbs
--------------------------------------------------------------------------------
In normal usage you must call `Breadcrumbs::render($name, $params...)` to render the breadcrumbs on every page. If you prefer, you can name your breadcrumbs the same as your routes and avoid this duplication.
### Setup
#### Name your routes
Make sure each of your routes has a name. For example (`routes/web.php`):
```php
// Home
Route::get('/', 'HomeController@index')->name('home');
// Home > [Page]
Route::get('/page/{id}', 'PageController@show')->name('page');
```
For more details see [Named Routes](https://laravel.com/docs/5.3/routing#named-routes) in the Laravel documentation.
#### Name your breadcrumbs to match
For each route, create a breadcrumb with the same name. For example (`routes/breadcrumbs.php`):
```php
// Home
Breadcrumbs::register('home', function($breadcrumbs) {
$breadcrumbs->push('Home', route('home'));
});
// Home > [Page]
Breadcrumbs::register('page', function($breadcrumbs, $id)
{
$page = Page::findOrFail($id);
$breadcrumbs->parent('home');
$breadcrumbs->push($page->title, route('page', $page->id));
});
```
#### Output breadcrumbs in your layout
Call `Breadcrumbs::render()` with no parameters in your layout file (e.g. `resources/views/app.blade.php`):
```html+php
{!! Breadcrumbs::render() !!}
```
This will automatically output breadcrumbs corresponding to the current route.
It will throw an exception if the breadcrumb doesn't exist, to remind you to create one. To prevent this behaviour, change it to:
```html+php
{!! Breadcrumbs::renderIfExists() !!}
```
### Route model binding
Laravel Breadcrumbs uses the same model binding as the controller. For example:
```php
// routes/web.php
Route::model('page', 'Page');
Route::get('/page/{page}', ['uses' => 'PageController@show', 'as' => 'page']);
```
```php
// app/Http/Controllers/PageController.php
class PageController extends Controller
{
public function show($page)
{
return view('page/show', ['page' => $page]);
}
}
```
```php
// routes/breadcrumbs.php
Breadcrumbs::register('page', function($breadcrumbs, $page)
{
$breadcrumbs->parent('home');
$breadcrumbs->push($page->title, route('page', $page->id));
});
```
This makes your code less verbose and more efficient by only loading the page from the database once.
For more details see [Route Model Binding](https://laravel.com/docs/5.3/routing#route-model-binding) in the Laravel documentation.
### Resourceful controllers
Laravel automatically creates route names for resourceful controllers, e.g. `photo.index`, which you can use when defining your breadcrumbs. For example:
```php
// routes/web.php
Route::resource('photo', 'PhotoController');
```
```
$ php artisan route:list
+--------+----------+--------------------+---------------+-------------------------+------------+
| Domain | Method | URI | Name | Action | Middleware |
+--------+----------+--------------------+---------------+-------------------------+------------+
| | GET|HEAD | photo | photo.index | PhotoController@index | |
| | GET|HEAD | photo/create | photo.create | PhotoController@create | |
| | POST | photo | photo.store | PhotoController@store | |
| | GET|HEAD | photo/{photo} | photo.show | PhotoController@show | |
| | GET|HEAD | photo/{photo}/edit | photo.edit | PhotoController@edit | |
| | PUT | photo/{photo} | photo.update | PhotoController@update | |
| | PATCH | photo/{photo} | | PhotoController@update | |
| | DELETE | photo/{photo} | photo.destroy | PhotoController@destroy | |
+--------+----------+--------------------+---------------+-------------------------+------------+
```
```php
// routes/breadcrumbs.php
// Photos
Breadcrumbs::register('photo.index', function($breadcrumbs)
{
$breadcrumbs->parent('home');
$breadcrumbs->push('Photos', route('photo.index'));
});
// Photos > Upload Photo
Breadcrumbs::register('photo.create', function($breadcrumbs)
{
$breadcrumbs->parent('photo.index');
$breadcrumbs->push('Upload Photo', route('photo.create'));
});
// Photos > [Photo Name]
Breadcrumbs::register('photo.show', function($breadcrumbs, $photo)
{
$breadcrumbs->parent('photo.index');
$breadcrumbs->push($photo->title, route('photo.show', $photo->id));
});
// Photos > [Photo Name] > Edit Photo
Breadcrumbs::register('photo.edit', function($breadcrumbs, $photo)
{
$breadcrumbs->parent('photo.show', $photo);
$breadcrumbs->push('Edit Photo', route('photo.edit', $photo->id));
});
```
For more details see [Resource Controllers](https://laravel.com/docs/5.3/controllers#resource-controllers) in the Laravel documentation.
### Implicit controllers
To use implicit controllers, you must specify names for each route. For example:
```php
// routes/web.php
Route::controller('auth', 'Auth\AuthController', [
'getRegister' => 'auth.register',
'getLogin' => 'auth.login',
]);
```
(You don't need to provide route names for actions that redirect and never display a view e.g. most POST views.)
For more details see [Implicit Controllers](https://laravel.com/docs/5.1/controllers#implicit-controllers) in the Laravel documentation.
Advanced Usage
--------------------------------------------------------------------------------
### Breadcrumbs with no URL
The second parameter to `push()` is optional, so if you want a breadcrumb with no URL you can do so:
```php
$breadcrumbs->push('Sample');
```
The `$breadcrumb->url` value will be `null`.
The default Twitter Bootstrap templates provided render this with a CSS class of "active", the same as the last breadcrumb, because otherwise they default to black text not grey which doesn't look right.
### Custom data
The `push()` method accepts an optional third parameter, `$data` an array of arbitrary data to be passed to the breadcrumb, which you can use in your custom template. For example, if you wanted each breadcrumb to have an icon, you could do:
```php
$breadcrumbs->push('Home', '/', ['icon' => 'home.png']);
```
The `$data` array's entries will be merged into the breadcrumb as properties, so you would access the icon as ``$breadcrumb->icon`` in your template, like this:
```html+php
<li><a href="{{ $breadcrumb->url }}">
<img src="/images/icons/{{ $breadcrumb->icon }}">
{{ $breadcrumb->title }}
</a></li>
```
Do not use the following keys in your data array, as they will be overwritten: `title`, `url`, `first`, `last`.
### Defining breadcrumbs in a different file
If you don't want to use `routes/breadcrumbs.php` (or `app/Http/breadcrumbs.php` in Laravel 5.2 and below), you can create a custom service provider to use instead of `DaveJamesMiller\Breadcrumbs\ServiceProvider` and override the `registerBreadcrumbs()` method:
```php
<?php
namespace App\Providers;
use DaveJamesMiller\Breadcrumbs\ServiceProvider;
class BreadcrumbsServiceProvider extends ServiceProvider
{
public function registerBreadcrumbs()
{
require base_path('path/to/breadcrumbs.php');
}
}
```
If you are creating your own package, simply load them from your service provider's `boot()` method:
```php
class MyServiceProvider extends ServiceProvider
{
public function register() {}
public function boot()
{
if (class_exists('Breadcrumbs'))
require __DIR__ . '/breadcrumbs.php';
}
}
```
### Switching views dynamically
You can change the view at runtime by calling:
```php
Breadcrumbs::setView('view.name');
```
Or you can call `Breadcrumbs::generate()` and then load the view manually:
```html+php
@include('_partials/breadcrumbs2', ['breadcrumbs' => Breadcrumbs::generate('category', $category)])
```
### Overriding the "current" route
If you call `Breadcrumbs::render()` or `Breadcrumbs::generate()` with no parameters, it will use the current route name and parameters by default (as returned by Laravel's `Route::current()` method).
You can override this by calling `Breadcrumbs::setCurrentRoute($name, $param1, $param2...)` or `Breadcrumbs::setCurrentRouteArray($name, $params)`.
### Passing an array of parameters
If the breadcrumb requires multiple parameters, you would normally pass them like this:
```php
Breadcrumbs::render('name', $param1, $param2, $param3);
Breadcrumbs::generate('name', $param1, $param2, $param3);
$breadcrumbs->parent('name', $param1, $param2, $param3);
```
If you want to pass an array of parameters instead you can use these methods:
```php
Breadcrumbs::renderArray('name', $params);
Breadcrumbs::generateArray('name', $params);
$breadcrumbs->parentArray('name', $params);
```
### Checking if a breadcrumb exists
By default an exception will be thrown if the breadcrumb doesn't exist, so you know to add it. If you want suppress this you can call the following methods instead:
- `Breadcrumbs::renderIfExists()` (returns an empty string)
- `Breadcrumbs::renderIfExistsArray()` (returns an empty string) (was `renderArrayIfExists` before 3.0.0)
- `Breadcrumbs::generateIfExists()` (returns an empty array)
- `Breadcrumbs::generateIfExistsArray()` (returns an empty array) (was `generateArrayIfExists` before 3.0.0)
Alternatively you can call `Breadcrumbs::exists('name')`, which returns a boolean.
API Reference
--------------------------------------------------------------------------------
### `Breadcrumbs` Facade
| Method | Returns | Added in |
|------------------------------------------------------|-----------|----------|
| `Breadcrumbs::register($name, $callback)` | *(none)* | 1.0.0 |
| `Breadcrumbs::exists()` | boolean | 2.2.0 |
| `Breadcrumbs::exists($name)` | boolean | 2.2.0 |
| `Breadcrumbs::generate()` | array | 2.2.3 |
| `Breadcrumbs::generate($name)` | array | 1.0.0 |
| `Breadcrumbs::generate($name, $param1, ...)` | array | 1.0.0 |
| `Breadcrumbs::generateArray($name, $params)` | array | 2.0.0 |
| `Breadcrumbs::generateIfExists()` | array | 2.2.0 |
| `Breadcrumbs::generateIfExists($name)` | array | 2.2.0 |
| `Breadcrumbs::generateIfExists($name, $param1, ...)` | array | 2.2.0 |
| `Breadcrumbs::generateIfExistsArray($name, $params)` | array | 3.0.0 |
| `Breadcrumbs::render()` | string | 2.2.0 |
| `Breadcrumbs::render($name)` | string | 1.0.0 |
| `Breadcrumbs::render($name, $param1, ...)` | string | 1.0.0 |
| `Breadcrumbs::renderArray($name, $params)` | string | 2.0.0 |
| `Breadcrumbs::renderIfExists()` | string | 2.2.0 |
| `Breadcrumbs::renderIfExists($name)` | string | 2.2.0 |
| `Breadcrumbs::renderIfExists($name, $param1, ...)` | string | 2.2.0 |
| `Breadcrumbs::renderIfExistsArray($name, $params)` | string | 3.0.0 |
| `Breadcrumbs::setCurrentRoute($name)` | *(none)* | 2.2.0 |
| `Breadcrumbs::setCurrentRoute($name, $param1, ...)` | *(none)* | 2.2.0 |
| `Breadcrumbs::setCurrentRouteArray($name, $params)` | *(none)* | 2.2.0 |
| `Breadcrumbs::clearCurrentRoute()` | *(none)* | 2.2.0 |
| `Breadcrumbs::setView($view)` | *(none)* | 1.0.0 |
[Source](https://github.com/davejamesmiller/laravel-breadcrumbs/blob/develop/src/Manager.php)
### Defining breadcrumbs
```php
Breadcrumbs::register('name', function($breadcrumbs, $page) {
// ...
});
```
| Method | Returns | Added in |
|------------------------------------------------------|-----------|----------|
| `$breadcrumbs->push($title)` | *(none)* | 1.0.0 |
| `$breadcrumbs->push($title, $url)` | *(none)* | 1.0.0 |
| `$breadcrumbs->push($title, $url, $data)` | *(none)* | 2.3.0 |
| `$breadcrumbs->parent($name)` | *(none)* | 1.0.0 |
| `$breadcrumbs->parent($name, $param1, ...)` | *(none)* | 1.0.0 |
| `$breadcrumbs->parentArray($name, $params)` | *(none)* | 2.0.0 |
[Source](https://github.com/davejamesmiller/laravel-breadcrumbs/blob/develop/src/Generator.php)
### In the view (template)
`$breadcrumbs` (array), contains:
| Variable | Type | Added in |
|--------------------------------------|---------------|----------|
| `$breadcrumb->title` | string | 1.0.0 |
| `$breadcrumb->url` | string / null | 1.0.0 |
| `$breadcrumb->first` | boolean | 1.0.0 |
| `$breadcrumb->last` | boolean | 1.0.0 |
| `$breadcrumb->custom_attribute_name` | mixed | 2.3.0 |
Changelog
--------------------------------------------------------------------------------
*Laravel Breadcrumbs uses [Semantic Versioning](http://semver.org/).*
### [v3.0.3](https://github.com/davejamesmiller/laravel-breadcrumbs/tree/3.0.3) (24 Jun 2017)
- Fix exception when using `renderIfExists()` (and related methods) with an unnamed route
([#133](https://github.com/davejamesmiller/laravel-breadcrumbs/issues/133))
- Convert docs back to Markdown
### [v3.0.2](https://github.com/davejamesmiller/laravel-breadcrumbs/tree/3.0.2) (30 Jan 2017)
- Laravel 5.4 support
### [v3.0.1](https://github.com/davejamesmiller/laravel-breadcrumbs/tree/3.0.1) (28 Aug 2016)
- Laravel 5.3 support
#### Upgrading from Laravel 5.2 to 5.3
- Upgrade Laravel Breadcrumbs to 3.0.1 (or above)
- Move `app/Http/breadcrumbs.php` to `routes/breadcrumbs.php` (optional but recommended)
### [v3.0.0](https://github.com/davejamesmiller/laravel-breadcrumbs/tree/3.0.0) (8 Feb 2015)
- Add Laravel 5 support
([#62](https://github.com/davejamesmiller/laravel-breadcrumbs/issues/62))
- Change view namespace from `laravel-breadcrumbs::` to `breadcrumbs::`
- Change Bootstrap 3 template from `<ul>` to `<ol>` to match the [documentation](http://getbootstrap.com/components/#breadcrumbs)
- Move documentation from GitHub (Markdown) to [Read The Docs](https://readthedocs.org/) (reStructuredText/[Sphinx](http://sphinx-doc.org/))
- Greatly improve unit & integration tests (largely thanks to [Testbench](https://github.com/orchestral/testbench))
- Fix issue that prevented non-deferred service providers referencing Breadcrumbs by making Breadcrumbs non-deferred also
([#39](https://github.com/davejamesmiller/laravel-breadcrumbs/issues/39))
- Rename `generateArrayIfExists()` to `generateIfExistsArray()`
- Rename `renderArrayIfExists()` to `renderIfExistsArray()`
- Remove `$breadcrumbs->get()` and `$breadcrumbs->set()` methods from Generator class (they were never used nor documented)
- Remove `Breadcrumbs::getView()`
- Switch from PSR-0 to PSR-4 file naming
#### Upgrading from 2.x to 3.x
- [Upgrade to Laravel 5](https://laravel.com/docs/5.0/upgrade#upgrade-5.0)
- Move `app/breadcrumbs.php` to `app/Http/breadcrumbs.php`
- Move `app/config/packages/davejamesmiller/laravel-breadcrumbs/config.php` to `config/breadcrumbs.php` (if used)
The following changes are optional because there are shims in place:
- In the config file, replace `laravel-breadcrumbs::` with `breadcrumbs::`
- Replace any calls to `Breadcrumbs::generateArrayIfExists()` with `Breadcrumbs::generateIfExistsArray()`
- Replace any calls to `Breadcrumbs::renderArrayIfExists()` with `Breadcrumbs::renderIfExistsArray()`
**Note:** Laravel 4 and PHP 5.3 are no longer supported please continue to use the [2.x branch](https://github.com/davejamesmiller/laravel-breadcrumbs/tree/2.x) if you use them.
### v2.x
[Changelog for 2.x and below](https://github.com/davejamesmiller/laravel-breadcrumbs/blob/2.x/CHANGELOG.md)
Technical Support
--------------------------------------------------------------------------------
Sorry, **I don't offer free technical support** for my open source packages. If you can't get Laravel Breadcrumbs working in your application, I suggest you try posting a question on [Stack Overflow](https://stackoverflow.com/search?q=laravel+breadcrumbs). For paid support / consultancy please [email me](https://davejamesmiller.com/contact).
Bug Reports
--------------------------------------------------------------------------------
Please note this is free software so **I don't guarantee to fix any bugs** I will investigate if/when I have the time and motivation to do so. Don't be afraid to go into the Laravel Breadcrumbs code (`vendor/davejamesmiller/laravel-breadcrumbs/src/`), use `var_dump()` to see what's happening and fix your own problems! For paid support / consultancy please [email me](https://davejamesmiller.com/contact).
Contributing
--------------------------------------------------------------------------------
**Bug fixes:** Please fix it and open a [pull request](https://github.com/davejamesmiller/laravel-breadcrumbs/pulls). Bonus points if you add a unit test to make sure it doesn't happen again!
**New features:** Only high value features with a clear use case and well-considered API will be accepted. They must be documented and include unit tests. I suggest you open an [issue](https://github.com/davejamesmiller/laravel-breadcrumbs/issues) to discuss the idea first.
**Documentation:** If you think the documentation can be improved in any way, please do [edit this file](https://github.com/davejamesmiller/laravel-breadcrumbs/edit/master/README.md) and make a pull request.
### Developing inside a real application
The easiest way to work on Laravel Breadcrumbs inside a real Laravel application is to tell Composer to install from source (Git) using the `--prefer-source` flag:
```bash
cd /path/to/repo
rm -rf vendor/davejamesmiller/laravel-breadcrumbs
composer install --prefer-source
```
Then:
```bash
cd vendor/davejamesmiller/laravel-breadcrumbs
git checkout -t origin/master
git checkout -b YOUR_BRANCH
# Make changes and commit them
git remote add YOUR_USERNAME git@github.com:YOUR_USERNAME/laravel-breadcrumbs
git push -u YOUR_USERNAME YOUR_BRANCH
```
Alternatively there is a [test app](https://github.com/davejamesmiller/laravel-breadcrumbs-test) that you can use.
### Unit tests
To run the unit tests, simply run:
```bash
cd /path/to/laravel-breadcrumbs
composer update
scripts/test.sh
```
#### Code coverage
To check code coverage, you will also need [Xdebug](https://xdebug.org/) installed. Run:
```bash
scripts/test-coverage.sh
```
Then open `test-coverage/index.html` to view the results. Be aware of the [edge cases](https://phpunit.de/manual/current/en/code-coverage-analysis.html#code-coverage-analysis.edge-cases) in PHPUnit that can make it not-quite-accurate.
### Using your fork in a project
To use your own fork in a project, update the `composer.json` in your main project as follows:
```json
{
"repositories": [
{
"type": "vcs",
"url": "https://github.com/YOUR_USERNAME/laravel-breadcrumbs.git"
}
],
"require": {
"davejamesmiller/laravel-breadcrumbs": "dev-YOUR_BRANCH"
}
}
```
Replace `YOUR_USERNAME` with your GitHub username and `YOUR_BRANCH` with the branch name (e.g. `develop`). This tells Composer to use your repository instead of the default one.
License
--------------------------------------------------------------------------------
*[MIT License](https://choosealicense.com/licenses/mit/)*
**Copyright © 2013-2017 Dave James Miller**
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

View File

@@ -1,7 +0,0 @@
# Release Checklist
- [ ] Ensure the documentation is up to date, particularly the changelog
- [ ] Merge the changes into the `master` branch (if necessary)
- [ ] Push the code changes to GitHub (`git push`)
- [ ] Tag the release (`git tag 1.2.3`)
- [ ] Push the tag (`git push --tag`)

View File

@@ -1,31 +1,50 @@
{
"name": "davejamesmiller/laravel-breadcrumbs",
"description": "A simple Laravel-style way to create breadcrumbs in Laravel.",
"keywords": [
"laravel"
],
"homepage": "https://laravel-breadcrumbs.readthedocs.io/",
"authors": [
{
"name": "Dave James Miller",
"email": "dave@davejamesmiller.com",
"homepage": "https://davejamesmiller.com/"
}
],
"license": "MIT License",
"require": {
"php": ">=5.4.0",
"illuminate/support": "5.*",
"illuminate/view": "5.*"
},
"require-dev": {
"mockery/mockery": "0.9.*",
"orchestra/testbench": "3.2.*|3.3.*",
"phpunit/phpunit": "4.*"
},
"autoload": {
"psr-4": {
"DaveJamesMiller\\Breadcrumbs\\": "src/"
}
}
"name": "davejamesmiller/laravel-breadcrumbs",
"description": "A simple Laravel-style way to create breadcrumbs.",
"keywords": [
"laravel"
],
"homepage": "https://github.com/davejamesmiller/laravel-breadcrumbs",
"authors": [
{
"name": "Dave James Miller",
"email": "dave@davejamesmiller.com"
}
],
"license": "MIT",
"require": {
"php": ">=7.1.3",
"illuminate/support": ">=5.6",
"illuminate/view": ">=5.6",
"facade/ignition-contracts": "^1.0"
},
"require-dev": {
"orchestra/testbench": ">=3.6",
"phpunit/phpunit": "^7.0|^8.0",
"php-coveralls/php-coveralls": "^2.0",
"spatie/phpunit-snapshot-assertions": "^2.0"
},
"minimum-stability": "dev",
"prefer-stable": true,
"autoload": {
"psr-4": {
"DaveJamesMiller\\Breadcrumbs\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"App\\": "tests/",
"BreadcrumbsTests\\": "tests/"
}
},
"extra": {
"laravel": {
"providers": [
"DaveJamesMiller\\Breadcrumbs\\BreadcrumbsServiceProvider"
],
"aliases": {
"Breadcrumbs": "DaveJamesMiller\\Breadcrumbs\\Facades\\Breadcrumbs"
}
}
}
}

View File

@@ -2,6 +2,74 @@
return [
'view' => 'breadcrumbs::bootstrap3',
/*
|--------------------------------------------------------------------------
| View Name
|--------------------------------------------------------------------------
|
| Choose a view to display when Breadcrumbs::render() is called.
| Built in templates are:
|
| - 'breadcrumbs::bootstrap4' - Bootstrap 4
| - 'breadcrumbs::bootstrap3' - Bootstrap 3
| - 'breadcrumbs::bootstrap2' - Bootstrap 2
| - 'breadcrumbs::bulma' - Bulma
| - 'breadcrumbs::foundation6' - Foundation 6
| - 'breadcrumbs::materialize' - Materialize
| - 'breadcrumbs::uikit' - UIkit
| - 'breadcrumbs::json-ld' - JSON-LD Structured Data
|
| Or a custom view, e.g. '_partials/breadcrumbs'.
|
*/
'view' => 'breadcrumbs::bootstrap4',
/*
|--------------------------------------------------------------------------
| Breadcrumbs File(s)
|--------------------------------------------------------------------------
|
| The file(s) where breadcrumbs are defined. e.g.
|
| - base_path('routes/breadcrumbs.php')
| - glob(base_path('breadcrumbs/*.php'))
|
*/
'files' => base_path('routes/breadcrumbs.php'),
/*
|--------------------------------------------------------------------------
| Exceptions
|--------------------------------------------------------------------------
|
| Determine when to throw an exception.
|
*/
// When route-bound breadcrumbs are used but the current route doesn't have a name (UnnamedRouteException)
'unnamed-route-exception' => true,
// When route-bound breadcrumbs are used and the matching breadcrumb doesn't exist (InvalidBreadcrumbException)
'missing-route-bound-breadcrumb-exception' => true,
// When a named breadcrumb is used but doesn't exist (InvalidBreadcrumbException)
'invalid-named-breadcrumb-exception' => true,
/*
|--------------------------------------------------------------------------
| Classes
|--------------------------------------------------------------------------
|
| Subclass the default classes for more advanced customisations.
|
*/
// Manager
'manager-class' => DaveJamesMiller\Breadcrumbs\BreadcrumbsManager::class,
// Generator
'generator-class' => DaveJamesMiller\Breadcrumbs\BreadcrumbsGenerator::class,
];

View File

@@ -1,34 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
backupGlobals="false"
backupStaticAttributes="false"
beStrictAboutOutputDuringTests="true"
beStrictAboutTestsThatDoNotTestAnything="true"
bootstrap="tests/bootstrap.php"
checkForUnintentionallyCoveredCode="true"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
verbose="true"
>
<!-- List of files with tests inside -->
<testsuites>
<testsuite name="Package Test Suite">
<directory suffix=".php">tests/unit/</directory>
<directory suffix=".php">tests/integration/</directory>
</testsuite>
</testsuites>
<!-- List of source files for code coverage checker -->
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">src/</directory>
</whitelist>
</filter>
</phpunit>

View File

@@ -1,9 +0,0 @@
#!/bin/bash
set -o nounset -o pipefail -o errexit
cd "$(dirname "$0")/.."
################################################################################
# Run PHPUnit with code coverage (requires Xdebug).
################################################################################
exec php -d xdebug.coverage_enable=On vendor/bin/phpunit --coverage-html test-coverage "$@"

View File

@@ -1,9 +0,0 @@
#!/bin/bash
set -o nounset -o pipefail -o errexit
cd "$(dirname "$0")/.."
################################################################################
# Run PHPUnit.
################################################################################
exec vendor/bin/phpunit "$@"

View File

@@ -0,0 +1,12 @@
<?php
namespace DaveJamesMiller\Breadcrumbs;
use Exception;
/**
* Base class for exceptions in Laravel Breadcrumbs.
*/
abstract class BreadcrumbsException extends Exception
{
}

View File

@@ -0,0 +1,98 @@
<?php
namespace DaveJamesMiller\Breadcrumbs;
use DaveJamesMiller\Breadcrumbs\Exceptions\InvalidBreadcrumbException;
use Illuminate\Support\Collection;
/**
* Generate a set of breadcrumbs for a page.
*
* This is passed as the first parameter to all breadcrumb-generating closures. In the documentation it is named
* `$breadcrumbs`.
*/
class BreadcrumbsGenerator
{
/**
* @var Collection Breadcrumbs currently being generated.
*/
protected $breadcrumbs;
/**
* @var array The registered breadcrumb-generating callbacks.
*/
protected $callbacks = [];
/**
* Generate breadcrumbs.
*
* @param array $callbacks The registered breadcrumb-generating closures.
* @param array $before The registered 'before' callbacks.
* @param array $after The registered 'after' callbacks.
* @param string $name The name of the current page.
* @param array $params The parameters to pass to the closure for the current page.
* @return Collection The generated breadcrumbs.
* @throws InvalidBreadcrumbException if the name is (or any ancestor names are) not registered.
*/
public function generate(array $callbacks, array $before, array $after, string $name, array $params): Collection
{
$this->breadcrumbs = new Collection;
$this->callbacks = $callbacks;
foreach ($before as $callback) {
$callback($this);
}
$this->call($name, $params);
foreach ($after as $callback) {
$callback($this);
}
return $this->breadcrumbs;
}
/**
* Call the closure to generate breadcrumbs for a page.
*
* @param string $name The name of the page.
* @param array $params The parameters to pass to the closure.
* @throws InvalidBreadcrumbException if the name is not registered.
*/
protected function call(string $name, array $params): void
{
if (! isset($this->callbacks[ $name ])) {
throw new InvalidBreadcrumbException($name);
}
$this->callbacks[$name]($this, ...$params);
}
/**
* Add breadcrumbs for a parent page.
*
* Should be called from the closure for a page, before `push()` is called.
*
* @param string $name The name of the parent page.
* @param array ...$params The parameters to pass to the closure.
* @throws InvalidBreadcrumbException
*/
public function parent(string $name, ...$params): void
{
$this->call($name, $params);
}
/**
* Add a breadcrumb.
*
* Should be called from the closure for each page. May be called more than once.
*
* @param string $title The title of the page.
* @param string|null $url The URL of the page.
* @param array $data Optional associative array of additional data to pass to the view.
*/
public function push(string $title, string $url = null, array $data = []): void
{
$this->breadcrumbs->push((object) array_merge($data, compact('title', 'url')));
}
}

View File

@@ -0,0 +1,315 @@
<?php
namespace DaveJamesMiller\Breadcrumbs;
use DaveJamesMiller\Breadcrumbs\Exceptions\DuplicateBreadcrumbException;
use DaveJamesMiller\Breadcrumbs\Exceptions\InvalidBreadcrumbException;
use DaveJamesMiller\Breadcrumbs\Exceptions\UnnamedRouteException;
use DaveJamesMiller\Breadcrumbs\Exceptions\ViewNotSetException;
use Illuminate\Contracts\View\Factory as ViewFactory;
use Illuminate\Routing\Router;
use Illuminate\Support\Collection;
use Illuminate\Support\HtmlString;
use Illuminate\Support\Traits\Macroable;
/**
* The main Breadcrumbs singleton class, responsible for registering, generating and rendering breadcrumbs.
*/
class BreadcrumbsManager
{
use Macroable;
/**
* @var BreadcrumbsGenerator
*/
protected $generator;
/**
* @var Router
*/
protected $router;
/**
* @var ViewFactory
*/
protected $viewFactory;
/**
* @var array The registered breadcrumb-generating callbacks.
*/
protected $callbacks = [];
/**
* @var array Closures to call before generating breadcrumbs for the current page.
*/
protected $before = [];
/**
* @var array Closures to call after generating breadcrumbs for the current page.
*/
protected $after = [];
/**
* @var array|null The current route name and parameters.
*/
protected $route;
public function __construct(BreadcrumbsGenerator $generator, Router $router, ViewFactory $viewFactory)
{
$this->generator = $generator;
$this->router = $router;
$this->viewFactory = $viewFactory;
}
/**
* Register a breadcrumb-generating callback for a page.
*
* @param string $name The name of the page.
* @param callable $callback The callback, which should accept a Generator instance as the first parameter and may
* accept additional parameters.
* @return void
* @throws \DaveJamesMiller\Breadcrumbs\Exceptions\DuplicateBreadcrumbException If the given name has already been
* used.
*/
public function for(string $name, callable $callback): void
{
if (isset($this->callbacks[$name])) {
throw new DuplicateBreadcrumbException($name);
}
$this->callbacks[$name] = $callback;
}
/**
* Register a breadcrumb-generating callback for a page.
*
* For backwards-compatibility with v5.0.0 and below.
*
* @param string $name The name of the page.
* @param callable $callback The callback, which should accept a Generator instance as the first parameter and may
* accept additional parameters.
* @return void
* @throws \DaveJamesMiller\Breadcrumbs\Exceptions\DuplicateBreadcrumbException If the given name has already been
* used.
* @see self::for()
*/
public function register(string $name, callable $callback): void
{
$this->for($name, $callback);
}
/**
* Register a closure to call before generating breadcrumbs for the current page.
*
* For example, this can be used to always prepend the homepage without needing to manually add it to each page.
*
* @param callable $callback The callback, which should accept a Generator instance as the first and only parameter.
* @return void
*/
public function before(callable $callback): void
{
$this->before[] = $callback;
}
/**
* Register a closure to call after generating breadcrumbs for the current page.
*
* For example, this can be used to append the current page number when using pagination.
*
* @param callable $callback The callback, which should accept a Generator instance as the first and only parameter.
* @return void
*/
public function after(callable $callback): void
{
$this->after[] = $callback;
}
/**
* Check if a breadcrumb with the given name exists.
*
* If no name is given, defaults to the current route name.
*
* @param string|null $name The page name.
* @return bool Whether there is a registered callback with that name.
*/
public function exists(string $name = null): bool
{
if (is_null($name)) {
try {
[$name] = $this->getCurrentRoute();
} catch (UnnamedRouteException $e) {
return false;
}
}
return isset($this->callbacks[$name]);
}
/**
* Generate a set of breadcrumbs for a page.
*
* @param string|null $name The name of the current page.
* @param mixed ...$params The parameters to pass to the closure for the current page.
* @return \Illuminate\Support\Collection The generated breadcrumbs.
* @throws \DaveJamesMiller\Breadcrumbs\Exceptions\UnnamedRouteException if no name is given and the current route
* doesn't have an associated name.
* @throws \DaveJamesMiller\Breadcrumbs\Exceptions\InvalidBreadcrumbException if the name is (or any ancestor names
* are) not registered.
*/
public function generate(string $name = null, ...$params): Collection
{
$origName = $name;
// Route-bound breadcrumbs
if ($name === null) {
try {
[$name, $params] = $this->getCurrentRoute();
} catch (UnnamedRouteException $e) {
if (config('breadcrumbs.unnamed-route-exception')) {
throw $e;
}
return new Collection;
}
}
// Generate breadcrumbs
try {
return $this->generator->generate($this->callbacks, $this->before, $this->after, $name, $params);
} catch (InvalidBreadcrumbException $e) {
if ($origName === null && config('breadcrumbs.missing-route-bound-breadcrumb-exception')) {
$e->setIsRouteBound();
throw $e;
}
if ($origName !== null && config('breadcrumbs.invalid-named-breadcrumb-exception')) {
throw $e;
}
return new Collection;
}
}
/**
* Render breadcrumbs for a page with the specified view.
*
* @param string $view The name of the view to render.
* @param string|null $name The name of the current page.
* @param mixed ...$params The parameters to pass to the closure for the current page.
* @return \Illuminate\Support\HtmlString The generated HTML.
* @throws \DaveJamesMiller\Breadcrumbs\Exceptions\InvalidBreadcrumbException if the name is (or any ancestor names are) not registered.
* @throws \DaveJamesMiller\Breadcrumbs\Exceptions\UnnamedRouteException if no name is given and the current route doesn't have an associated name.
* @throws \DaveJamesMiller\Breadcrumbs\Exceptions\ViewNotSetException if no view has been set.
*/
public function view(string $view, string $name = null, ...$params): HtmlString
{
$breadcrumbs = $this->generate($name, ...$params);
// TODO: After dropping support for Laravel 5.8 and below, change this to return the view directly
// https://github.com/laravel/framework/pull/29600
$html = $this->viewFactory->make($view, compact('breadcrumbs'))->render();
return new HtmlString($html);
}
/**
* Render breadcrumbs for a page with the default view.
*
* @param string|null $name The name of the current page.
* @param mixed ...$params The parameters to pass to the closure for the current page.
* @return \Illuminate\Support\HtmlString The generated HTML.
* @throws \DaveJamesMiller\Breadcrumbs\Exceptions\InvalidBreadcrumbException if the name is (or any ancestor names are) not registered.
* @throws \DaveJamesMiller\Breadcrumbs\Exceptions\UnnamedRouteException if no name is given and the current route doesn't have an associated name.
* @throws \DaveJamesMiller\Breadcrumbs\Exceptions\ViewNotSetException if no view has been set.
*/
public function render(string $name = null, ...$params): HtmlString
{
$view = config('breadcrumbs.view');
if (!$view) {
throw new ViewNotSetException('Breadcrumbs view not specified (check config/breadcrumbs.php)');
}
return $this->view($view, $name, ...$params);
}
/**
* Get the last breadcrumb for the current page.
*
* Optionally pass a
*
* @return \stdClass|null The breadcrumb for the current page.
* @throws \DaveJamesMiller\Breadcrumbs\Exceptions\UnnamedRouteException if the current route doesn't have an associated name.
* @throws \DaveJamesMiller\Breadcrumbs\Exceptions\InvalidBreadcrumbException if the name is (or any ancestor names are) not registered.
*/
public function current(): ?\stdClass
{
return $this->generate()->where('current', '!==', false)->last();
}
/**
* Get the current route name and parameters.
*
* This may be the route set manually with the setCurrentRoute() method, but normally is the route retrieved from
* the Laravel Router.
*
* #### Example
* ```php
* [$name, $params] = $this->getCurrentRoute();
* ```
*
* @return array A two-element array consisting of the route name (string) and any parameters (array).
* @throws \DaveJamesMiller\Breadcrumbs\Exceptions\UnnamedRouteException if the current route doesn't have an associated name.
*/
protected function getCurrentRoute()
{
// Manually set route
if ($this->route) {
return $this->route;
}
// Determine the current route
$route = $this->router->current();
// No current route - must be the 404 page
if ($route === null) {
return ['errors.404', []];
}
// Convert route to name
$name = $route->getName();
if ($name === null) {
throw new UnnamedRouteException($route);
}
// Get the current route parameters
$params = array_values($route->parameters());
return [$name, $params];
}
/**
* Set the current route name and parameters to use when calling render() or generate() with no parameters.
*
* @param string $name The name of the current page.
* @param mixed ...$params The parameters to pass to the closure for the current page.
* @return void
*/
public function setCurrentRoute(string $name, ...$params): void
{
$this->route = [$name, $params];
}
/**
* Clear the previously set route name and parameters to use when calling render() or generate() with no parameters.
*
* Next time it will revert to the default behaviour of using the current route from Laravel.
*
* @return void
*/
public function clearCurrentRoute(): void
{
$this->route = null;
}
}

View File

@@ -0,0 +1,97 @@
<?php
namespace DaveJamesMiller\Breadcrumbs;
// Not available until Laravel 5.8
//use Illuminate\Contracts\Support\DeferrableProvider;
use Illuminate\Support\ServiceProvider;
/**
* The Laravel service provider, which registers, configures and bootstraps the package.
*/
class BreadcrumbsServiceProvider extends ServiceProvider //implements DeferrableProvider
{
public function isDeferred()
{
// Remove this and uncomment DeferrableProvider after dropping support
// for Laravel 5.7 and below
return true;
}
/**
* Get the services provided for deferred loading.
*
* @return array
*/
public function provides(): array
{
return [BreadcrumbsManager::class];
}
/**
* Register the service provider.
*
* @return void
*/
public function register(): void
{
// Load the default config values
$configFile = __DIR__ . '/../config/breadcrumbs.php';
$this->mergeConfigFrom($configFile, 'breadcrumbs');
// Publish the config/breadcrumbs.php file
$this->publishes([$configFile => config_path('breadcrumbs.php')], 'breadcrumbs-config');
// Register Manager class singleton with the app container
$this->app->singleton(BreadcrumbsManager::class, config('breadcrumbs.manager-class'));
// Register Generator class so it can be overridden
$this->app->bind(BreadcrumbsGenerator::class, config('breadcrumbs.generator-class'));
// Register 'breadcrumbs::' view namespace
$this->loadViewsFrom(__DIR__ . '/../views/', 'breadcrumbs');
}
/**
* Bootstrap the application events.
*
* @return void
*/
public function boot(): void
{
// Load the routes/breadcrumbs.php file
$this->registerBreadcrumbs();
}
/**
* Load the routes/breadcrumbs.php file (if it exists) which registers available breadcrumbs.
*
* This method can be overridden in a child class. It is called by the boot() method, which Laravel calls
* automatically when bootstrapping the application.
*
* @return void
*/
public function registerBreadcrumbs(): void
{
// Load the routes/breadcrumbs.php file, or other configured file(s)
$files = config('breadcrumbs.files');
if (! $files) {
return;
}
// If it is set to the default value and that file doesn't exist, skip loading it rather than causing an error
if ($files === base_path('routes/breadcrumbs.php') && ! is_file($files)) {
return;
}
// Support both Breadcrumbs:: and $breadcrumbs-> syntax by making $breadcrumbs variable available
/** @noinspection PhpUnusedLocalVariableInspection */
$breadcrumbs = $this->app->make(BreadcrumbsManager::class);
// Support both a single string filename and an array of filenames (e.g. returned by glob())
foreach ((array) $files as $file) {
require $file;
}
}
}

View File

@@ -1,47 +0,0 @@
<?php namespace DaveJamesMiller\Breadcrumbs;
use Illuminate\Contracts\Routing\Registrar as Router;
class CurrentRoute {
protected $route;
protected $router;
public function __construct(Router $router)
{
$this->router = $router;
}
public function get()
{
if ($this->route)
return $this->route;
$route = $this->router->current();
if (is_null($route))
return ['', []];
$name = $route->getName();
if (is_null($name)) {
$uri = head($route->methods()) . ' /' . $route->uri();
throw new Exception("The current route ($uri) is not named - please check routes.php for an \"as\" parameter");
}
$params = array_values($route->parameters());
return [$name, $params];
}
public function set($name, $params)
{
$this->route = [$name, $params];
}
public function clear()
{
$this->route = null;
}
}

View File

@@ -1,4 +0,0 @@
<?php namespace DaveJamesMiller\Breadcrumbs;
class Exception extends \Exception {
}

View File

@@ -0,0 +1,51 @@
<?php
namespace DaveJamesMiller\Breadcrumbs\Exceptions;
use DaveJamesMiller\Breadcrumbs\BreadcrumbsException;
use Facade\IgnitionContracts\Solution;
use Facade\IgnitionContracts\BaseSolution;
use Facade\IgnitionContracts\ProvidesSolution;
use Illuminate\Support\Str;
/**
* Exception that is thrown if the user attempts to register two breadcrumbs with the same name.
*
* @see \DaveJamesMiller\Breadcrumbs\BreadcrumbsManager::register()
*/
class DuplicateBreadcrumbException extends BreadcrumbsException implements ProvidesSolution
{
private $name;
public function __construct($name)
{
parent::__construct("Breadcrumb name \"{$name}\" has already been registered");
$this->name = $name;
}
public function getSolution(): Solution
{
// Determine the breadcrumbs file name(s)
$files = (array)config('breadcrumbs.files');
$basePath = base_path() . DIRECTORY_SEPARATOR;
foreach ($files as &$file) {
$file = Str::replaceFirst($basePath, '', $file);
}
if (count($files) === 1) {
$description = "Look in `$files[0]` for multiple breadcrumbs named `{$this->name}`.";
} else {
$description = "Look in the following files for multiple breadcrumbs named `{$this->name}`:\n\n- `" . implode("`\n -`", $files) . '`';
}
$links = [];
$links['Defining breadcrumbs'] = 'https://github.com/davejamesmiller/laravel-breadcrumbs#defining-breadcrumbs';
$links['Laravel Breadcrumbs documentation'] = 'https://github.com/davejamesmiller/laravel-breadcrumbs#laravel-breadcrumbs';
return BaseSolution::create('Remove the duplicate breadcrumb')
->setSolutionDescription($description)
->setDocumentationLinks($links);
}
}

View File

@@ -0,0 +1,72 @@
<?php
namespace DaveJamesMiller\Breadcrumbs\Exceptions;
use DaveJamesMiller\Breadcrumbs\BreadcrumbsException;
use Facade\IgnitionContracts\BaseSolution;
use Facade\IgnitionContracts\ProvidesSolution;
use Facade\IgnitionContracts\Solution;
use Illuminate\Support\Facades\Request;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Str;
/**
* Exception that is thrown if the user attempts to generate breadcrumbs for a page that is not registered.
*/
class InvalidBreadcrumbException extends BreadcrumbsException implements ProvidesSolution
{
private $name;
private $isRouteBound = false;
public function __construct($name)
{
parent::__construct("Breadcrumb not found with name \"{$name}\"");
$this->name = $name;
}
public function setIsRouteBound()
{
$this->isRouteBound = true;
}
public function getSolution(): Solution
{
// Determine the breadcrumbs file name
$files = (array)config('breadcrumbs.files');
if (count($files) === 1) {
$file = Str::replaceFirst(base_path() . DIRECTORY_SEPARATOR, '', $files[0]);
} else {
$file = 'one of the files defined in config/breadcrumbs.php';
}
// Determine the current route name
$route = Route::current();
$routeName = $route ? $route->getName() : null;
if ($routeName) {
$url = "route('{$this->name}')";
} else {
$url = "url('" . Request::path() . "')";
}
$links = [];
$links['Defining breadcrumbs'] = 'https://github.com/davejamesmiller/laravel-breadcrumbs#defining-breadcrumbs';
if ($this->isRouteBound) {
$links['Route-bound breadcrumbs'] = 'https://github.com/davejamesmiller/laravel-breadcrumbs#route-bound-breadcrumbs';
}
$links['Silencing breadcrumb exceptions'] = 'https://github.com/davejamesmiller/laravel-breadcrumbs#configuration-file';
$links['Laravel Breadcrumbs documentation'] = 'https://github.com/davejamesmiller/laravel-breadcrumbs#laravel-breadcrumbs';
return BaseSolution::create("Add this to $file")
->setSolutionDescription("
```php
Breadcrumbs::for('{$this->name}', function (\$trail) {
\$trail->push('Title Here', $url);
});
```")
->setDocumentationLinks($links);
}
}

View File

@@ -0,0 +1,63 @@
<?php
namespace DaveJamesMiller\Breadcrumbs\Exceptions;
use DaveJamesMiller\Breadcrumbs\BreadcrumbsException;
use Facade\IgnitionContracts\BaseSolution;
use Facade\IgnitionContracts\ProvidesSolution;
use Facade\IgnitionContracts\Solution;
use Illuminate\Routing\Route;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Str;
/**
* Exception that is thrown if the user attempt to render breadcrumbs for the current route but the current route
* doesn't have a name.
*/
class UnnamedRouteException extends BreadcrumbsException implements ProvidesSolution
{
/**
* @var Route
*/
private $route;
public function __construct(Route $route)
{
$uri = Arr::first($route->methods()) . ' /' . ltrim($route->uri(), '/');
parent::__construct("The current route ($uri) is not named");
$this->route = $route;
}
public function getSolution(): Solution
{
$method = strtolower(Arr::first($this->route->methods()));
$uri = $this->route->uri();
$action = $this->route->getActionName();
if ($action === '\Illuminate\Routing\ViewController') {
$method = 'view';
$action = "'" . ($this->route->defaults['view'] ?? 'view-name') . "'";
} elseif ($action === 'Closure') {
$action = "function() {\n ...\n}";
} else {
$action = "'" . Str::replaceFirst(App::getNamespace() . 'Http\Controllers\\', '', $action) . "'";
}
$links = [];
$links['Route-bound breadcrumbs'] = 'https://github.com/davejamesmiller/laravel-breadcrumbs#route-bound-breadcrumbs';
$links['Silencing breadcrumb exceptions'] = 'https://github.com/davejamesmiller/laravel-breadcrumbs#configuration-file';
$links['Laravel Breadcrumbs documentation'] = 'https://github.com/davejamesmiller/laravel-breadcrumbs#laravel-breadcrumbs';
return BaseSolution::create('Give the route a name')
->setSolutionDescription("For example:
```php
Route::$method('$uri', $action)->name('sample-name');
```")
->setDocumentationLinks($links);
}
}

View File

@@ -0,0 +1,25 @@
<?php
namespace DaveJamesMiller\Breadcrumbs\Exceptions;
use DaveJamesMiller\Breadcrumbs\BreadcrumbsException;
use Facade\IgnitionContracts\Solution;
use Facade\IgnitionContracts\BaseSolution;
use Facade\IgnitionContracts\ProvidesSolution;
/**
* Exception that is thrown if the user attempts to render breadcrumbs without setting a view.
*/
class ViewNotSetException extends BreadcrumbsException implements ProvidesSolution
{
public function getSolution(): Solution
{
$links = [];
$links['Choosing a breadcrumbs template (view)'] = 'https://github.com/davejamesmiller/laravel-breadcrumbs#3-choose-a-template';
$links['Laravel Breadcrumbs documentation'] = 'https://github.com/davejamesmiller/laravel-breadcrumbs#laravel-breadcrumbs';
return BaseSolution::create('Set a view for Laravel Breadcrumbs')
->setSolutionDescription("Please check `config/breadcrumbs.php` for a valid `'view'` (e.g. `'breadcrumbs::bootstrap4'`)")
->setDocumentationLinks($links);
}
}

View File

@@ -1,18 +0,0 @@
<?php namespace DaveJamesMiller\Breadcrumbs;
use Illuminate\Support\Facades\Facade as BaseFacade;
class Facade extends BaseFacade {
/**
* Get the registered name of the component.
*
* @return string
*/
protected static function getFacadeAccessor()
{
// App::make('breadcrumbs')
return 'breadcrumbs';
}
}

View File

@@ -0,0 +1,37 @@
<?php
namespace DaveJamesMiller\Breadcrumbs\Facades;
use DaveJamesMiller\Breadcrumbs\BreadcrumbsManager;
use Illuminate\Support\Facades\Facade;
/**
* Breadcrumbs facade - allows easy access to the Manager instance.
*
* @method static void for(string $name, callable $callback)
* @method static void register(string $name, callable $callback)
* @method static void before(callable $callback)
* @method static void after(callable $callback)
* @method static bool exists(string $name = NULL)
* @method static \Illuminate\Support\Collection generate(string $name = NULL, ...$params)
* @method static \Illuminate\Support\HtmlString view(string $view, string $name = NULL, ...$params)
* @method static \Illuminate\Support\HtmlString render(string $name = NULL, ...$params)
* @method static \stdClass|null current()
* @method static array getCurrentRoute()
* @method static void setCurrentRoute(string $name, ...$params)
* @method static void clearCurrentRoute()
* @mixin \Illuminate\Support\Traits\Macroable
* @see BreadcrumbsManager
*/
class Breadcrumbs extends Facade
{
/**
* Get the name of the class registered in the Application container.
*
* @return string
*/
protected static function getFacadeAccessor(): string
{
return BreadcrumbsManager::class;
}
}

View File

@@ -1,63 +0,0 @@
<?php namespace DaveJamesMiller\Breadcrumbs;
class Generator {
protected $breadcrumbs = [];
protected $callbacks = [];
public function generate(array $callbacks, $name, $params)
{
$this->breadcrumbs = [];
$this->callbacks = $callbacks;
$this->call($name, $params);
return $this->toArray();
}
protected function call($name, $params)
{
if (!isset($this->callbacks[$name]))
throw new Exception("Breadcrumb not found with name \"{$name}\"");
array_unshift($params, $this);
call_user_func_array($this->callbacks[$name], $params);
}
public function parent($name)
{
$params = array_slice(func_get_args(), 1);
$this->call($name, $params);
}
public function parentArray($name, $params = [])
{
$this->call($name, $params);
}
public function push($title, $url = null, array $data = [])
{
$this->breadcrumbs[] = (object) array_merge($data, [
'title' => $title,
'url' => $url,
// These will be altered later where necessary:
'first' => false,
'last' => false,
]);
}
public function toArray()
{
$breadcrumbs = $this->breadcrumbs;
// Add first & last indicators
if ($breadcrumbs) {
$breadcrumbs[0]->first = true;
$breadcrumbs[count($breadcrumbs) - 1]->last = true;
}
return $breadcrumbs;
}
}

View File

@@ -1,170 +0,0 @@
<?php namespace DaveJamesMiller\Breadcrumbs;
class Manager {
protected $currentRoute;
protected $generator;
protected $view;
protected $callbacks = [];
protected $viewName;
protected $currentRouteManual;
public function __construct(CurrentRoute $currentRoute, Generator $generator, View $view)
{
$this->generator = $generator;
$this->currentRoute = $currentRoute;
$this->view = $view;
}
public function register($name, $callback)
{
if (isset($this->callbacks[$name]))
throw new Exception("Breadcrumb name \"{$name}\" has already been registered");
$this->callbacks[$name] = $callback;
}
public function exists($name = null)
{
if (is_null($name)) {
try {
list($name) = $this->currentRoute->get();
} catch (Exception $e) {
return false;
}
}
return isset($this->callbacks[$name]);
}
public function generate($name = null)
{
if (is_null($name))
list($name, $params) = $this->currentRoute->get();
else
$params = array_slice(func_get_args(), 1);
return $this->generator->generate($this->callbacks, $name, $params);
}
public function generateArray($name, $params = [])
{
return $this->generator->generate($this->callbacks, $name, $params);
}
public function generateIfExists($name = null)
{
if (is_null($name)) {
try {
list($name, $params) = $this->currentRoute->get();
} catch (Exception $e) {
return [];
}
} else {
$params = array_slice(func_get_args(), 1);
}
if (!$this->exists($name))
return [];
return $this->generator->generate($this->callbacks, $name, $params);
}
public function generateIfExistsArray($name, $params = [])
{
if (!$this->exists($name))
return [];
return $this->generator->generate($this->callbacks, $name, $params);
}
/**
* @deprecated Since 3.0.0
* @see generateIfExistsArray
*/
public function generateArrayIfExists()
{
return call_user_func_array([$this, 'generateIfExistsArray'], func_get_args());
}
public function render($name = null)
{
if (is_null($name))
list($name, $params) = $this->currentRoute->get();
else
$params = array_slice(func_get_args(), 1);
$breadcrumbs = $this->generator->generate($this->callbacks, $name, $params);
return $this->view->render($this->viewName, $breadcrumbs);
}
public function renderArray($name, $params = [])
{
$breadcrumbs = $this->generator->generate($this->callbacks, $name, $params);
return $this->view->render($this->viewName, $breadcrumbs);
}
public function renderIfExists($name = null)
{
if (is_null($name)) {
try {
list($name, $params) = $this->currentRoute->get();
} catch (Exception $e) {
return '';
}
} else {
$params = array_slice(func_get_args(), 1);
}
if (!$this->exists($name))
return '';
$breadcrumbs = $this->generator->generate($this->callbacks, $name, $params);
return $this->view->render($this->viewName, $breadcrumbs);
}
public function renderIfExistsArray($name, $params = [])
{
if (!$this->exists($name))
return '';
$breadcrumbs = $this->generator->generate($this->callbacks, $name, $params);
return $this->view->render($this->viewName, $breadcrumbs);
}
/**
* @deprecated Since 3.0.0
* @see renderIfExistsArray
*/
public function renderArrayIfExists()
{
return call_user_func_array([$this, 'renderIfExistsArray'], func_get_args());
}
public function setCurrentRoute($name)
{
$params = array_slice(func_get_args(), 1);
$this->currentRoute->set($name, $params);
}
public function setCurrentRouteArray($name, $params = [])
{
$this->currentRoute->set($name, $params);
}
public function clearCurrentRoute()
{
$this->currentRoute->clear();
}
public function setView($view)
{
$this->viewName = $view;
}
}

View File

@@ -1,83 +0,0 @@
<?php namespace DaveJamesMiller\Breadcrumbs;
use Illuminate\Support\ServiceProvider as BaseServiceProvider;
class ServiceProvider extends BaseServiceProvider {
/**
* Indicates if loading of the provider is deferred.
*
* @var bool
*/
// Can't enable this because there appears to be a bug in Laravel where a
// non-deferred service provider can't use a deferred one because the boot
// method is not called - see DependantServiceProviderTest.
// protected $defer = true;
/**
* Get the services provided by the provider.
*
* @return array
*/
public function provides()
{
return ['breadcrumbs'];
}
/**
* Register the service provider.
*
* @return void
*/
public function register()
{
$this->app->singleton('breadcrumbs', function ($app)
{
$breadcrumbs = $this->app->make('DaveJamesMiller\Breadcrumbs\Manager');
$viewPath = __DIR__ . '/../views/';
$this->loadViewsFrom($viewPath, 'breadcrumbs');
$this->loadViewsFrom($viewPath, 'laravel-breadcrumbs'); // Backwards-compatibility with 2.x
$breadcrumbs->setView($app['config']['breadcrumbs.view']);
return $breadcrumbs;
});
}
/**
* Bootstrap the application events.
*
* @return void
*/
public function boot()
{
$configFile = __DIR__ . '/../config/breadcrumbs.php';
$this->mergeConfigFrom($configFile, 'breadcrumbs');
$this->publishes([
$configFile => config_path('breadcrumbs.php')
]);
$this->registerBreadcrumbs();
}
// This method can be overridden in a child class
public function registerBreadcrumbs()
{
// Load the app breadcrumbs if they're in routes/breadcrumbs.php (Laravel 5.3)
if (file_exists($file = $this->app['path.base'].'/routes/breadcrumbs.php'))
{
require $file;
}
// Load the app breadcrumbs if they're in app/Http/breadcrumbs.php (Laravel 5.0-5.2)
elseif (file_exists($file = $this->app['path'].'/Http/breadcrumbs.php'))
{
require $file;
}
}
}

View File

@@ -1,22 +0,0 @@
<?php namespace DaveJamesMiller\Breadcrumbs;
use Illuminate\Contracts\View\Factory as ViewFactory;
class View {
protected $factory;
public function __construct(ViewFactory $factory)
{
$this->factory = $factory;
}
public function render($view, $breadcrumbs)
{
if (!$view)
throw new Exception('Breadcrumbs view not specified (check the view in config/breadcrumbs.php, and ensure DaveJamesMiller\Breadcrumbs\ServiceProvider is loaded before any dependants in config/app.php)');
return $this->factory->make($view, compact('breadcrumbs'))->render();
}
}

View File

@@ -1,39 +0,0 @@
<?php
abstract class TestCase extends Orchestra\Testbench\TestCase {
protected function getPackageProviders($app)
{
return [
DaveJamesMiller\Breadcrumbs\ServiceProvider::class,
];
}
protected function getPackageAliases($app)
{
return [
'Breadcrumbs' => DaveJamesMiller\Breadcrumbs\Facade::class
];
}
public function setUp()
{
parent::setUp();
$this->loadServiceProvider();
}
protected function loadServiceProvider()
{
// Need to trigger register() to test the views
$this->app->make('breadcrumbs');
}
public function tearDown()
{
$this->addToAssertionCount(Mockery::getContainer()->mockery_getExpectationCount());
Mockery::close();
}
}

View File

@@ -1,7 +0,0 @@
<?php
// Load Composer packages
require __DIR__ . '/../vendor/autoload.php';
// Load base class
require __DIR__ . '/../tests/TestCase.php';

View File

@@ -1,3 +0,0 @@
<ol class="breadcrumb">
<li class="active">Home</li>
</ol>

View File

@@ -1,3 +0,0 @@
<ol class="breadcrumb">
<li class="active">Home</li>
</ol>

View File

@@ -1,17 +0,0 @@
<ul class="breadcrumb">
<li>
<a href="/">Home</a>
<span class="divider">/</span>
</li>
<li class="active">
Not a link
<span class="divider">/</span>
</li>
<li>
<a href="/blog">Blog &amp; &lt; &gt;</a>
<span class="divider">/</span>
</li>
<li class="active">
Sample Post
</li>
</ul>

View File

@@ -1,6 +0,0 @@
<ol class="breadcrumb">
<li><a href="/">Home</a></li>
<li class="active">Not a link</li>
<li><a href="/blog">Blog &amp; &lt; &gt;</a></li>
<li class="active">Sample Post</li>
</ol>

View File

@@ -1,5 +0,0 @@
<ol class="breadcrumb">
<li><a href="/">Home</a></li>
<li><a href="/category/456">Sample Category</a></li>
<li class="active">Sample Post</li>
</ol>

View File

@@ -1,29 +0,0 @@
<?php
class CustomServiceProviderTest extends TestCase {
protected function getPackageProviders($app)
{
return [
CustomServiceProvider::class,
];
}
public function testRender()
{
$html = Breadcrumbs::render('home');
$this->assertXmlStringEqualsXmlFile(__DIR__ . '/../fixtures/CustomServiceProvider.html', $html);
}
}
class CustomServiceProvider extends DaveJamesMiller\Breadcrumbs\ServiceProvider {
public function registerBreadcrumbs()
{
Breadcrumbs::register('home', function($breadcrumbs) {
$breadcrumbs->push('Home', '/');
});
}
}

View File

@@ -1,43 +0,0 @@
<?php
class DependantServiceProviderErrorTest extends TestCase {
protected function getPackageProviders($app)
{
return [
// These are in the wrong order
DependantServiceProviderError::class,
DaveJamesMiller\Breadcrumbs\ServiceProvider::class,
];
}
protected function loadServiceProvider()
{
// Disabled - we want to test the automatic loading instead
}
/**
* @expectedException DaveJamesMiller\Breadcrumbs\Exception
* @expectedExceptionMessage Breadcrumbs view not specified
*/
public function testRender()
{
Breadcrumbs::render('home');
}
}
class DependantServiceProviderError extends Illuminate\Support\ServiceProvider {
public function register()
{
}
public function boot()
{
Breadcrumbs::register('home', function($breadcrumbs) {
$breadcrumbs->push('Home', '/');
});
}
}

View File

@@ -1,39 +0,0 @@
<?php
class DependantServiceProviderTest extends TestCase {
protected function getPackageProviders($app)
{
return [
DaveJamesMiller\Breadcrumbs\ServiceProvider::class,
DependantServiceProvider::class,
];
}
protected function loadServiceProvider()
{
// Disabled - we want to test the automatic loading instead
}
public function testRender()
{
$html = Breadcrumbs::render('home');
$this->assertXmlStringEqualsXmlFile(__DIR__ . '/../fixtures/DependantServiceProvider.html', $html);
}
}
class DependantServiceProvider extends Illuminate\Support\ServiceProvider {
public function register()
{
}
public function boot()
{
Breadcrumbs::register('home', function($breadcrumbs) {
$breadcrumbs->push('Home', '/');
});
}
}

View File

@@ -1,61 +0,0 @@
<?php
class IntegrationTest extends TestCase {
public function setUp()
{
parent::setUp();
Breadcrumbs::register('home', function($breadcrumbs) {
$breadcrumbs->push('Home', '/');
});
Breadcrumbs::register('category', function($breadcrumbs, $category) {
$breadcrumbs->parent('home');
$breadcrumbs->push($category->title, '/category/' . $category->id);
});
Breadcrumbs::register('post', function($breadcrumbs, $post) {
$breadcrumbs->parent('category', $post->category);
$breadcrumbs->push($post->title, '/blog/' . $post->id);
});
$this->post = (object) [
'id' => 123,
'title' => 'Sample Post',
'category' => (object) [
'id' => 456,
'title' => 'Sample Category',
],
];
}
public function testGenerate()
{
$breadcrumbs = Breadcrumbs::generate('post', $this->post);
$this->assertCount(3, $breadcrumbs);
$this->assertSame('Home', $breadcrumbs[0]->title);
$this->assertSame('/', $breadcrumbs[0]->url);
$this->assertTrue($breadcrumbs[0]->first);
$this->assertFalse($breadcrumbs[0]->last);
$this->assertSame('Sample Category', $breadcrumbs[1]->title);
$this->assertSame('/category/456', $breadcrumbs[1]->url);
$this->assertFalse($breadcrumbs[1]->first);
$this->assertFalse($breadcrumbs[1]->last);
$this->assertSame('Sample Post', $breadcrumbs[2]->title);
$this->assertSame('/blog/123', $breadcrumbs[2]->url);
$this->assertFalse($breadcrumbs[2]->first);
$this->assertTrue($breadcrumbs[2]->last);
}
public function testRender()
{
$html = Breadcrumbs::render('post', $this->post);
$this->assertXmlStringEqualsXmlFile(__DIR__ . '/../fixtures/integration.html', $html);
}
}

View File

@@ -1,81 +0,0 @@
<?php
use DaveJamesMiller\Breadcrumbs\CurrentRoute;
use Mockery as m;
class CurrentRouteTest extends TestCase {
public function setUp()
{
parent::setUp();
$this->currentRoute = app('DaveJamesMiller\Breadcrumbs\CurrentRoute');
}
public function testNamedRoute()
{
Route::get('/sample', ['as' => 'sampleroute', function()
{
$this->assertSame(['sampleroute', []], $this->currentRoute->get());
}]);
$this->call('GET', '/sample');
}
public function testNamedRouteWithParameters()
{
$object = new stdClass;
Route::bind('object', function() use ($object) {
return $object;
});
Route::get('/sample/{text}/{object}', ['as' => 'sampleroute', function() use ($object)
{
$this->assertSame(['sampleroute', ['blah', $object]], $this->currentRoute->get());
}]);
$this->call('GET', '/sample/blah/object');
}
/**
* @expectedException DaveJamesMiller\Breadcrumbs\Exception
* @expectedExceptionMessage The current route (GET /sample/unnamed) is not named - please check routes.php for an "as" parameter
*/
public function testUnnamedRoute()
{
Route::get('/sample/unnamed', function()
{
$this->currentRoute->get();
});
// Laravel 5.3+ catches the exception
throw $this->call('GET', '/sample/unnamed')->exception;
}
public function testSet()
{
$this->currentRoute->set('custom', [1, 'blah']);
Route::get('/sample', ['as' => 'sampleroute', function()
{
$this->assertSame(['custom', [1, 'blah']], $this->currentRoute->get());
}]);
$this->call('GET', '/sample');
}
public function testClear()
{
$this->currentRoute->set('custom', [1, 'blah']);
$this->currentRoute->clear();
Route::get('/sample', ['as' => 'sampleroute', function()
{
$this->assertSame(['sampleroute', []], $this->currentRoute->get());
}]);
$this->call('GET', '/sample');
}
}

View File

@@ -1,11 +0,0 @@
<?php
class FacadeTest extends TestCase {
public function testFacade()
{
$this->assertInstanceOf('DaveJamesMiller\Breadcrumbs\Manager', Breadcrumbs::getFacadeRoot());
$this->assertSame($this->app['breadcrumbs'], Breadcrumbs::getFacadeRoot());
}
}

View File

@@ -1,189 +0,0 @@
<?php
use DaveJamesMiller\Breadcrumbs\Generator;
use Mockery as m;
class GeneratorTest extends TestCase {
public function setUp()
{
parent::setUp();
$this->generator = new Generator;
}
public function testCallbacks()
{
$this->generator->generate([
'sample' => function($breadcrumbs)
{
$this->assertSame($this->generator, $breadcrumbs);
},
], 'sample', []);
}
public function testCallbackParameters()
{
$this->generator->generate([
'sample' => function($breadcrumbs, $num, $text)
{
$this->assertSame(1, $num);
$this->assertSame('blah', $text);
},
], 'sample', [1, 'blah']);
}
// $breadcrumbs->push($title)
// $breadcrumb->title
public function testPush_title()
{
$breadcrumbs = $this->generator->generate([
'sample' => function($breadcrumbs)
{
$breadcrumbs->push('Home');
},
], 'sample', []);
$this->assertCount(1, $breadcrumbs);
$this->assertSame('Home', $breadcrumbs[0]->title);
$this->assertNull($breadcrumbs[0]->url);
}
// $breadcrumbs->push($title, $url)
// $breadcrumb->url
public function testPush_title_url()
{
$breadcrumbs = $this->generator->generate([
'sample' => function($breadcrumbs)
{
$breadcrumbs->push('Home', '/');
},
], 'sample', []);
$this->assertCount(1, $breadcrumbs);
$this->assertSame('Home', $breadcrumbs[0]->title);
$this->assertSame('/', $breadcrumbs[0]->url);
}
// $breadcrumbs->push($title, $url, $data)
// $breadcrumb->custom_attribute_name
public function testPush_title_url_data()
{
$data = [
'foo' => 'bar',
'baz' => 'qux',
'title' => 'should not be overwritten by custom data',
];
$breadcrumbs = $this->generator->generate([
'sample' => function($breadcrumbs)
{
$breadcrumbs->push('Home', '/', ['foo' => 'bar', 'title' => 'ignored']);
},
], 'sample', []);
$this->assertCount(1, $breadcrumbs);
$this->assertSame('Home', $breadcrumbs[0]->title);
$this->assertSame('/', $breadcrumbs[0]->url);
$this->assertSame('bar', $breadcrumbs[0]->foo);
}
public function testPushMultipleTimes()
{
$breadcrumbs = $this->generator->generate([
'sample' => function($breadcrumbs)
{
$breadcrumbs->push('Level 1', '/1');
$breadcrumbs->push('Level 2', '/2');
$breadcrumbs->push('Level 3', '/3');
},
], 'sample', []);
$this->assertCount(3, $breadcrumbs);
$this->assertSame('Level 1', $breadcrumbs[0]->title);
$this->assertSame('Level 2', $breadcrumbs[1]->title);
$this->assertSame('Level 3', $breadcrumbs[2]->title);
$this->assertSame('/1', $breadcrumbs[0]->url);
$this->assertSame('/2', $breadcrumbs[1]->url);
$this->assertSame('/3', $breadcrumbs[2]->url);
}
// $breadcrumbs->parent($name)
public function testParent_name()
{
$breadcrumbs = $this->generator->generate([
'home' => function($breadcrumbs)
{
$breadcrumbs->push('Home', '/');
},
'sample' => function($breadcrumbs)
{
$breadcrumbs->parent('home');
$breadcrumbs->push('Page', '/page');
},
], 'sample', []);
$this->assertCount(2, $breadcrumbs);
$this->assertSame('Home', $breadcrumbs[0]->title);
$this->assertSame('/', $breadcrumbs[0]->url);
$this->assertSame('Page', $breadcrumbs[1]->title);
$this->assertSame('/page', $breadcrumbs[1]->url);
}
// $breadcrumbs->parent($name, $param1, ...)
public function testParent_name_params()
{
$breadcrumbs = $this->generator->generate([
'parent' => function($breadcrumbs, $num, $text)
{
$this->assertSame(1, $num);
$this->assertSame('blah', $text);
},
'sample' => function($breadcrumbs)
{
$breadcrumbs->parent('parent', 1, 'blah');
},
], 'sample', []);
}
// $breadcrumbs->parentArray($name, $params)
public function testParentArray_name_params()
{
$breadcrumbs = $this->generator->generate([
'parent' => function($breadcrumbs, $num, $text)
{
$this->assertSame(1, $num);
$this->assertSame('blah', $text);
},
'sample' => function($breadcrumbs)
{
$breadcrumbs->parentArray('parent', [1, 'blah']);
},
], 'sample', []);
}
// $breadcrumb->first
// $breadcrumb->last
public function testFirstLast()
{
$breadcrumbs = $this->generator->generate([
'sample' => function($breadcrumbs)
{
$breadcrumbs->push('Level 1', '/1');
$breadcrumbs->push('Level 2', '/2');
$breadcrumbs->push('Level 3', '/3');
},
], 'sample', []);
$this->assertCount(3, $breadcrumbs);
$this->assertTrue($breadcrumbs[0]->first, '$breadcrumbs[0]->first');
$this->assertFalse($breadcrumbs[1]->first, '$breadcrumbs[1]->first');
$this->assertFalse($breadcrumbs[2]->first, '$breadcrumbs[2]->first');
$this->assertFalse($breadcrumbs[0]->last, '$breadcrumbs[0]->last');
$this->assertFalse($breadcrumbs[1]->last, '$breadcrumbs[1]->last');
$this->assertTrue($breadcrumbs[2]->last, '$breadcrumbs[2]->last');
}
}

View File

@@ -1,346 +0,0 @@
<?php
use DaveJamesMiller\Breadcrumbs\Manager;
use Mockery as m;
class ManagerTest extends TestCase {
public function setUp()
{
parent::setUp();
$this->currentRoute = m::mock('DaveJamesMiller\Breadcrumbs\CurrentRoute');
$this->generator = m::mock('DaveJamesMiller\Breadcrumbs\Generator');
$this->view = m::mock('DaveJamesMiller\Breadcrumbs\View');
$this->manager = new Manager($this->currentRoute, $this->generator, $this->view);
$this->manager->setView('view');
}
// Breadcrumbs::register($name, $callback) is tested by other methods
protected function register($name)
{
$this->manager->register($name, $fn = function() {
// We're not testing whether the callbacks are executed - see GeneratorTest
throw new Exception('Callback executed');
});
return $fn;
}
// Breadcrumbs::exists() -> boolean
public function testExists()
{
$this->currentRoute->shouldReceive('get')->andReturn(['sample', [1, 'blah']]);
$this->assertFalse($this->manager->exists());
$this->register('sample');
$this->assertTrue($this->manager->exists());
}
// Breadcrumbs::exists($name) -> boolean
public function testExists_name()
{
$this->assertFalse($this->manager->exists('sample'));
$this->register('sample');
$this->assertTrue($this->manager->exists('sample'));
}
// Breadcrumbs::generate() -> array
public function testGenerate()
{
$fn = $this->register('sample');
$this->currentRoute->shouldReceive('get')->andReturn(['sample', [1, 'blah']]);
$this->generator->shouldReceive('generate')->with(['sample' => $fn], 'sample', [1, 'blah'])->once()->andReturn('generated');;
$this->assertSame('generated', $this->manager->generate());
}
// Breadcrumbs::generate($name) -> array
public function testGenerate_name()
{
$fn = $this->register('sample');
$this->generator->shouldReceive('generate')->with(['sample' => $fn], 'sample', [])->once()->andReturn('generated');;
$this->assertSame('generated', $this->manager->generate('sample'));
}
// Breadcrumbs::generate($name, $param1, ...) -> array
public function testGenerate_name_params()
{
$fn = $this->register('sample');
$this->generator->shouldReceive('generate')->with(['sample' => $fn], 'sample', [1, 'blah'])->once()->andReturn('generated');;
$this->assertSame('generated', $this->manager->generate('sample', 1, 'blah'));
}
// Breadcrumbs::generateArray($name, $params) -> array
public function testGenerateArray_name_params()
{
$fn = $this->register('sample');
$this->generator->shouldReceive('generate')->with(['sample' => $fn], 'sample', [1, 'blah'])->once()->andReturn('generated');;
$this->assertSame('generated', $this->manager->generateArray('sample', [1, 'blah']));
}
// Breadcrumbs::generateIfExists() -> array
public function testGenerateIfExists_existing()
{
$fn = $this->register('sample');
$this->currentRoute->shouldReceive('get')->andReturn(['sample', [1, 'blah']]);
$this->generator->shouldReceive('generate')->with(['sample' => $fn], 'sample', [1, 'blah'])->once()->andReturn('generated');
$this->assertSame('generated', $this->manager->generateIfExists());
}
public function testGenerateIfExists_nonexistant()
{
$this->currentRoute->shouldReceive('get')->andReturn(['sample', [1, 'blah']]);
$this->generator->shouldReceive('generate')->never();
$this->assertSame([], $this->manager->generateIfExists());
}
public function testGenerateIfExists_noroute()
{
$this->currentRoute->shouldReceive('get')->andThrow('DaveJamesMiller\Breadcrumbs\Exception', 'The current route (GET /sample/unnamed) is not named - please check routes.php for an "as" parameter');
$this->generator->shouldReceive('generate')->never();
$this->assertSame([], $this->manager->generateIfExists());
}
// Breadcrumbs::generateIfExists($name) -> array
public function testGenerateIfExists_name_existing()
{
$fn = $this->register('sample');
$this->generator->shouldReceive('generate')->with(['sample' => $fn], 'sample', [])->once()->andReturn('generated');
$this->assertSame('generated', $this->manager->generateIfExists('sample'));
}
public function testGenerateIfExists_name_nonexistant()
{
$this->generator->shouldReceive('generate')->never();
$this->assertSame([], $this->manager->generateIfExists('sample'));
}
// Breadcrumbs::generateIfExists($name, $param1, ...) -> array
public function testGenerateIfExists_name_params_existing()
{
$fn = $this->register('sample');
$this->generator->shouldReceive('generate')->with(['sample' => $fn], 'sample', [1, 'blah'])->once()->andReturn('generated');
$this->assertSame('generated', $this->manager->generateIfExists('sample', 1, 'blah'));
}
public function testGenerateIfExists_name_params_nonexistant()
{
$this->generator->shouldReceive('generate')->never();
$this->assertSame([], $this->manager->generateIfExists('sample', 1, 'blah'));
}
// Breadcrumbs::generateArrayIfExists($name, $params) -> array
public function testGenerateArrayIfExists_name_params_existing()
{
$fn = $this->register('sample');
$this->generator->shouldReceive('generate')->with(['sample' => $fn], 'sample', [1, 'blah'])->once()->andReturn('generated');
$this->assertSame('generated', $this->manager->generateArrayIfExists('sample', [1, 'blah']));
}
public function testGenerateArrayIfExists_name_params_nonexistant()
{
$this->generator->shouldReceive('generate')->never();
$this->assertSame([], $this->manager->generateArrayIfExists('sample', [1, 'blah']));
}
// Breadcrumbs::render() -> array
public function testRender()
{
$fn = $this->register('sample');
$this->currentRoute->shouldReceive('get')->andReturn(['sample', [1, 'blah']]);
$this->generator->shouldReceive('generate')->with(['sample' => $fn], 'sample', [1, 'blah'])->once()->andReturn('generated');;
$this->view->shouldReceive('render')->with('view', 'generated')->once()->andReturn('rendered');
$this->assertSame('rendered', $this->manager->render());
}
// Breadcrumbs::render($name) -> array
public function testRender_name()
{
$fn = $this->register('sample');
$this->generator->shouldReceive('generate')->with(['sample' => $fn], 'sample', [])->once()->andReturn('generated');;
$this->view->shouldReceive('render')->with('view', 'generated')->once()->andReturn('rendered');
$this->assertSame('rendered', $this->manager->render('sample'));
}
// Breadcrumbs::render($name, $param1, ...) -> array
public function testRender_name_params()
{
$fn = $this->register('sample');
$this->generator->shouldReceive('generate')->with(['sample' => $fn], 'sample', [1, 'blah'])->once()->andReturn('generated');;
$this->view->shouldReceive('render')->with('view', 'generated')->once()->andReturn('rendered');
$this->assertSame('rendered', $this->manager->render('sample', 1, 'blah'));
}
// Breadcrumbs::renderArray($name, $params) -> array
public function testRenderArray_name_params()
{
$fn = $this->register('sample');
$this->generator->shouldReceive('generate')->with(['sample' => $fn], 'sample', [1, 'blah'])->once()->andReturn('generated');;
$this->view->shouldReceive('render')->with('view', 'generated')->once()->andReturn('rendered');
$this->assertSame('rendered', $this->manager->renderArray('sample', [1, 'blah']));
}
// Breadcrumbs::renderIfExists() -> array
public function testRenderIfExists_existing()
{
$fn = $this->register('sample');
$this->currentRoute->shouldReceive('get')->andReturn(['sample', [1, 'blah']]);
$this->generator->shouldReceive('generate')->with(['sample' => $fn], 'sample', [1, 'blah'])->once()->andReturn('generated');
$this->view->shouldReceive('render')->with('view', 'generated')->once()->andReturn('rendered');
$this->assertSame('rendered', $this->manager->renderIfExists());
}
public function testRenderIfExists_nonexistant()
{
$this->currentRoute->shouldReceive('get')->andReturn(['sample', [1, 'blah']]);
$this->generator->shouldReceive('generate')->never();
$this->view->shouldReceive('render')->never();
$this->assertSame('', $this->manager->renderIfExists());
}
public function testRenderIfExists_noroute()
{
$this->currentRoute->shouldReceive('get')->andThrow('DaveJamesMiller\Breadcrumbs\Exception', 'The current route (GET /sample/unnamed) is not named - please check routes.php for an "as" parameter');
$this->generator->shouldReceive('generate')->never();
$this->view->shouldReceive('render')->never();
$this->assertSame('', $this->manager->renderIfExists());
}
// Breadcrumbs::renderIfExists($name) -> array
public function testRenderIfExists_name_existing()
{
$fn = $this->register('sample');
$this->generator->shouldReceive('generate')->with(['sample' => $fn], 'sample', [])->once()->andReturn('generated');
$this->view->shouldReceive('render')->with('view', 'generated')->once()->andReturn('rendered');
$this->assertSame('rendered', $this->manager->renderIfExists('sample'));
}
public function testRenderIfExists_name_nonexistant()
{
$this->generator->shouldReceive('generate')->never();
$this->view->shouldReceive('render')->never();
$this->assertSame('', $this->manager->renderIfExists('sample'));
}
// Breadcrumbs::renderIfExists($name, $param1, ...) -> array
public function testRenderIfExists_name_params_existing()
{
$fn = $this->register('sample');
$this->generator->shouldReceive('generate')->with(['sample' => $fn], 'sample', [1, 'blah'])->once()->andReturn('generated');
$this->view->shouldReceive('render')->with('view', 'generated')->once()->andReturn('rendered');
$this->assertSame('rendered', $this->manager->renderIfExists('sample', 1, 'blah'));
}
public function testRenderIfExists_name_params_nonexistant()
{
$this->generator->shouldReceive('generate')->never();
$this->view->shouldReceive('render')->never();
$this->assertSame('', $this->manager->renderIfExists('sample', 1, 'blah'));
}
// Breadcrumbs::renderArrayIfExists($name, $params) -> array
public function testRenderArrayIfExists_name_params_existing()
{
$fn = $this->register('sample');
$this->generator->shouldReceive('generate')->with(['sample' => $fn], 'sample', [1, 'blah'])->once()->andReturn('generated');
$this->view->shouldReceive('render')->with('view', 'generated')->once()->andReturn('rendered');
$this->assertSame('rendered', $this->manager->renderArrayIfExists('sample', [1, 'blah']));
}
public function testRenderArrayIfExists_name_params_nonexistant()
{
$this->generator->shouldReceive('generate')->never();
$this->view->shouldReceive('render')->never();
$this->assertSame('', $this->manager->renderArrayIfExists('sample', [1, 'blah']));
}
// Breadcrumbs::setCurrentRoute($name)
public function testSetCurrentRoute_name()
{
$this->currentRoute->shouldReceive('set')->with('sample', [])->once();
$this->manager->setCurrentRoute('sample');
}
// Breadcrumbs::setCurrentRoute($name, $param1, ...)
public function testSetCurrentRoute_name_params()
{
$this->currentRoute->shouldReceive('set')->with('sample', [1, 'blah'])->once();
$this->manager->setCurrentRoute('sample', 1, 'blah');
}
// Breadcrumbs::setCurrentRouteArray($name, $params)
public function testSetCurrentRouteArray_name_params()
{
$this->currentRoute->shouldReceive('set')->with('sample', [1, 'blah'])->once();
$this->manager->setCurrentRouteArray('sample', [1, 'blah']);
}
// Breadcrumbs::clearCurrentRoute()
public function testClearCurrentRoute()
{
$this->currentRoute->shouldReceive('clear')->withNoArgs()->once();
$this->manager->clearCurrentRoute();
}
// Breadcrumbs::setView($view)
public function testSetView()
{
$fn = $this->register('sample');
$this->generator->shouldReceive('generate')->with(['sample' => $fn], 'sample', [1, 'blah'])->once()->andReturn('generated');;
$this->view->shouldReceive('render')->with('custom.view', 'generated')->once()->andReturn('rendered');
$this->manager->setView($view = 'custom.view');
$this->assertSame('rendered', $this->manager->render('sample', 1, 'blah'));
}
}

View File

@@ -1,51 +0,0 @@
<?php
class ViewTest extends TestCase {
public function setUp()
{
parent::setUp();
$this->view = app('DaveJamesMiller\Breadcrumbs\View');
$this->breadcrumbs = [
(object) [
'title' => 'Home',
'url' => '/',
'first' => true,
'last' => false,
],
(object) [
'title' => 'Not a link',
'url' => null, // Test non-links
'first' => false,
'last' => false,
],
(object) [
'title' => 'Blog & < >', // Test HTML escaping
'url' => '/blog',
'first' => false,
'last' => false,
],
(object) [
'title' => 'Sample Post',
'url' => '/blog/123',
'first' => false,
'last' => true,
],
];
}
public function testBootstrap2()
{
$html = $this->view->render('breadcrumbs::bootstrap2', $this->breadcrumbs);
$this->assertXmlStringEqualsXmlFile(__DIR__ . '/../fixtures/bootstrap2.html', $html);
}
public function testBootstrap3()
{
$html = $this->view->render('breadcrumbs::bootstrap3', $this->breadcrumbs);
$this->assertXmlStringEqualsXmlFile(__DIR__ . '/../fixtures/bootstrap3.html', $html);
}
}

View File

@@ -1,22 +1,30 @@
@if ($breadcrumbs)
<ul class="breadcrumb">
@foreach ($breadcrumbs as $breadcrumb)
@if ($breadcrumb->last)
<li class="active">
{{ $breadcrumb->title }}
</li>
@elseif ($breadcrumb->url)
<li>
<a href="{{ $breadcrumb->url }}">{{ $breadcrumb->title }}</a>
<span class="divider">/</span>
</li>
@else
{{-- Using .active to give it the right colour (grey by default) --}}
<li class="active">
{{ $breadcrumb->title }}
<span class="divider">/</span>
</li>
@endif
@endforeach
</ul>
@if (count($breadcrumbs))
<ul class="breadcrumb">
@foreach ($breadcrumbs as $breadcrumb)
@if ($loop->last)
<li class="active">
{{ $breadcrumb->title }}
</li>
@elseif ($breadcrumb->url)
<li>
<a href="{{ $breadcrumb->url }}">{{ $breadcrumb->title }}</a>
<span class="divider">/</span>
</li>
@else
{{-- Using .active to give it the right colour (grey by default) --}}
<li class="active">
{{ $breadcrumb->title }}
<span class="divider">/</span>
</li>
@endif
@endforeach
</ul>
@endif

View File

@@ -1,11 +1,15 @@
@if ($breadcrumbs)
<ol class="breadcrumb">
@foreach ($breadcrumbs as $breadcrumb)
@if ($breadcrumb->url && !$breadcrumb->last)
<li><a href="{{ $breadcrumb->url }}">{{ $breadcrumb->title }}</a></li>
@else
<li class="active">{{ $breadcrumb->title }}</li>
@endif
@endforeach
</ol>
@if (count($breadcrumbs))
<ol class="breadcrumb">
@foreach ($breadcrumbs as $breadcrumb)
@if ($breadcrumb->url && !$loop->last)
<li><a href="{{ $breadcrumb->url }}">{{ $breadcrumb->title }}</a></li>
@else
<li class="active">{{ $breadcrumb->title }}</li>
@endif
@endforeach
</ol>
@endif

View File

@@ -0,0 +1,15 @@
@if (count($breadcrumbs))
<ol class="breadcrumb">
@foreach ($breadcrumbs as $breadcrumb)
@if ($breadcrumb->url && !$loop->last)
<li class="breadcrumb-item"><a href="{{ $breadcrumb->url }}">{{ $breadcrumb->title }}</a></li>
@else
<li class="breadcrumb-item active">{{ $breadcrumb->title }}</li>
@endif
@endforeach
</ol>
@endif

View File

@@ -0,0 +1,25 @@
@if (count($breadcrumbs))
<nav class="breadcrumb" aria-label="breadcrumbs">
<ul>
@foreach ($breadcrumbs as $breadcrumb)
@if ($loop->last)
@if ($breadcrumb->url)
<li class="is-active"><a href="{{ $breadcrumb->url }}" aria-current="page">{{ $breadcrumb->title }}</a></li>
@else
<li class="is-active"><a aria-current="page">{{ $breadcrumb->title }}</a></li>
@endif
@else
@if ($breadcrumb->url)
<li><a href="{{ $breadcrumb->url }}">{{ $breadcrumb->title }}</a></li>
@else
<li class="is-active"><a>{{ $breadcrumb->title }}</a></li>
@endif
@endif
@endforeach
</ul>
</nav>
@endif

View File

@@ -0,0 +1,19 @@
@if (count($breadcrumbs))
<nav aria-label="You are here:" role="navigation">
<ul class="breadcrumbs">
@foreach ($breadcrumbs as $breadcrumb)
@if ($loop->last)
<li class="current"><span class="show-for-sr">Current:</span> {{ $breadcrumb->title }}</li>
@elseif ($breadcrumb->url)
<li><a href="{{ $breadcrumb->url }}">{{ $breadcrumb->title }}</a></li>
@else
<li class="disabled">{{ $breadcrumb->title }}</li>
@endif
@endforeach
</ul>
</nav>
@endif

View File

@@ -0,0 +1,23 @@
<?php
use Illuminate\Support\Facades\Request;
$json = [
'@context' => 'http://schema.org',
'@type' => 'BreadcrumbList',
'itemListElement' => [],
];
foreach ($breadcrumbs as $i => $breadcrumb) {
$json['itemListElement'][] = [
'@type' => 'ListItem',
'position' => $i + 1,
'item' => [
'@id' => $breadcrumb->url ?: Request::fullUrl(),
'name' => $breadcrumb->title,
'image' => $breadcrumb->image ?? null,
],
];
}
?>
<script type="application/ld+json"><?= json_encode($json) ?></script>

View File

@@ -0,0 +1,19 @@
@if (count($breadcrumbs))
<nav>
<div class="nav-wrapper">
<div class="col s12">
@foreach ($breadcrumbs as $breadcrumb)
@if ($breadcrumb->url && !$loop->last)
<a href="{{ $breadcrumb->url }}" class="breadcrumb">{{ $breadcrumb->title }}</a>
@else
<span class="breadcrumb">{{ $breadcrumb->title }}</span>
@endif
@endforeach
</div>
</div>
</nav>
@endif

View File

@@ -0,0 +1,13 @@
@if (count($breadcrumbs))
<ul class="uk-breadcrumb">
@foreach ($breadcrumbs as $breadcrumb)
@if ($loop->last)
<li><span>{{ $breadcrumb->title }}</span></li>
@elseif ($breadcrumb->url)
<li><a href="{{ $breadcrumb->url }}">{{ $breadcrumb->title }}</a></li>
@else
<li class="uk-disabled"><a>{{ $breadcrumb->title }}</a></li>
@endif
@endforeach
</ul>
@endif