⚗️ Ajoute un menu dynamique pour avoir la classe active automatique

This commit is contained in:
Clement Desmidt 2020-03-10 17:06:10 +01:00
parent 4bc1c97006
commit a9f40e55e7
6 changed files with 128 additions and 36 deletions

View File

@ -2,7 +2,28 @@
namespace App\Http; namespace App\Http;
use App\Http\Middleware\Authenticate;
use App\Http\Middleware\CheckForMaintenanceMode;
use App\Http\Middleware\EncryptCookies;
use App\Http\Middleware\GenerateMenus;
use App\Http\Middleware\RedirectIfAuthenticated;
use App\Http\Middleware\TrimStrings;
use App\Http\Middleware\TrustProxies;
use App\Http\Middleware\VerifyCsrfToken;
use Illuminate\Auth\Middleware\AuthenticateWithBasicAuth;
use Illuminate\Auth\Middleware\Authorize;
use Illuminate\Auth\Middleware\EnsureEmailIsVerified;
use Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse;
use Illuminate\Foundation\Http\Kernel as HttpKernel; use Illuminate\Foundation\Http\Kernel as HttpKernel;
use Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull;
use Illuminate\Foundation\Http\Middleware\ValidatePostSize;
use Illuminate\Http\Middleware\SetCacheHeaders;
use Illuminate\Routing\Middleware\SubstituteBindings;
use Illuminate\Routing\Middleware\ThrottleRequests;
use Illuminate\Routing\Middleware\ValidateSignature;
use Illuminate\Session\Middleware\AuthenticateSession;
use Illuminate\Session\Middleware\StartSession;
use Illuminate\View\Middleware\ShareErrorsFromSession;
class Kernel extends HttpKernel class Kernel extends HttpKernel
{ {
@ -14,11 +35,11 @@ class Kernel extends HttpKernel
* @var array * @var array
*/ */
protected $middleware = [ protected $middleware = [
\App\Http\Middleware\TrustProxies::class, TrustProxies::class,
\App\Http\Middleware\CheckForMaintenanceMode::class, CheckForMaintenanceMode::class,
\Illuminate\Foundation\Http\Middleware\ValidatePostSize::class, ValidatePostSize::class,
\App\Http\Middleware\TrimStrings::class, TrimStrings::class,
\Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class, ConvertEmptyStringsToNull::class,
]; ];
/** /**
@ -28,13 +49,14 @@ class Kernel extends HttpKernel
*/ */
protected $middlewareGroups = [ protected $middlewareGroups = [
'web' => [ 'web' => [
\App\Http\Middleware\EncryptCookies::class, EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class, AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class, StartSession::class,
// \Illuminate\Session\Middleware\AuthenticateSession::class, // \Illuminate\Session\Middleware\AuthenticateSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class, ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class, VerifyCsrfToken::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class, SubstituteBindings::class,
GenerateMenus::class,
], ],
'api' => [ 'api' => [
@ -51,15 +73,15 @@ class Kernel extends HttpKernel
* @var array * @var array
*/ */
protected $routeMiddleware = [ protected $routeMiddleware = [
'auth' => \App\Http\Middleware\Authenticate::class, 'auth' => Authenticate::class,
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class, 'auth.basic' => AuthenticateWithBasicAuth::class,
'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class, 'bindings' => SubstituteBindings::class,
'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class, 'cache.headers' => SetCacheHeaders::class,
'can' => \Illuminate\Auth\Middleware\Authorize::class, 'can' => Authorize::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class, 'guest' => RedirectIfAuthenticated::class,
'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class, 'signed' => ValidateSignature::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class, 'throttle' => ThrottleRequests::class,
'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class, 'verified' => EnsureEmailIsVerified::class,
]; ];
/** /**
@ -70,12 +92,12 @@ class Kernel extends HttpKernel
* @var array * @var array
*/ */
protected $middlewarePriority = [ protected $middlewarePriority = [
\Illuminate\Session\Middleware\StartSession::class, StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class, ShareErrorsFromSession::class,
\App\Http\Middleware\Authenticate::class, Authenticate::class,
\Illuminate\Routing\Middleware\ThrottleRequests::class, ThrottleRequests::class,
\Illuminate\Session\Middleware\AuthenticateSession::class, AuthenticateSession::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class, SubstituteBindings::class,
\Illuminate\Auth\Middleware\Authorize::class, Authorize::class,
]; ];
} }

View File

@ -0,0 +1,40 @@
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
use Lavary\Menu\Builder;
use Lavary\Menu\Menu;
class GenerateMenus
{
/**
* Handle an incoming request.
*
* @param Request $request
* @param Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
(new Menu)->make('menu', function (Builder $menu) {
$accueil = $menu->add('Accueil', ['route' => 'dashboard', 'class' => 'nav-item'])->nickname('home');
$accueil->link->attr(['class' => 'nav-link']);
$accueil->prepend('<i class="fe fe-home"></i> ');
$accueil->checkActivationStatus();
$stats = $menu->add('Mes statistiques', ['route' => 'stats', 'class' => 'nav-item'])->nickname('stats');
$stats->link->attr(['class' => 'nav-link']);
$stats->prepend('<i class="fe fe-trending-up"></i> ');
$stats->checkActivationStatus();
$gallery = $menu->add('Ma gallerie', ['route' => 'gallery', 'class' => 'nav-item'])->nickname('gallery');
$gallery->link->attr(['class' => 'nav-link']);
$gallery->prepend('<i class="fe fe-image"></i> ');
$gallery->checkActivationStatus();
});
return $next($request);
}
}

View File

@ -0,0 +1,13 @@
<?php
return [
'default' => [
'auto_activate' => true,
'activate_parents' => true,
'active_class' => 'active',
'restful' => false,
'cascade_data' => true,
'rest_base' => '', // string|array
'active_element' => 'link', // item|link
],
];

View File

@ -0,0 +1,5 @@
<?php
return [
'bootstrap-items' => 'laravel-menu::bootstrap-navbar-items',
];

View File

@ -101,15 +101,7 @@
</div> </div>
<div class="col-lg order-lg-first"> <div class="col-lg order-lg-first">
<ul class="nav nav-tabs border-0 flex-column flex-lg-row"> <ul class="nav nav-tabs border-0 flex-column flex-lg-row">
<li class="nav-item"> @include(config('laravel-menu.views.bootstrap-items'), ['items' => $menu->roots()])
<a href="./" class="nav-link active"><i class="fe fe-home"></i> {{ __('Accueil') }}</a>
</li>
<li class="nav-item dropdown">
<a href="#" class="nav-link"><i class="fe fe-trending-up"></i> {{ __('Mes statistiques') }}</a>
</li>
<li class="nav-item">
<a href="#" class="nav-link"><i class="fe fe-image"></i> {{ __('Ma gallerie') }}</a>
</li>
</ul> </ul>
</div> </div>
</div> </div>

View File

@ -0,0 +1,20 @@
@foreach($items as $item)
<li @lm_attrs($item) @if($item->hasChildren()) class="nav-item dropdown" @endif @lm_endattrs>
@if($item->link) <a @lm_attrs($item->link) @if($item->hasChildren()) class="nav-link dropdown-toggle" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" @else class="nav-link" @endif @lm_endattrs href="{!! $item->url() !!}">
{!! $item->title !!}
@if($item->hasChildren()) <b class="caret"></b> @endif
</a>
@else
<span class="navbar-text">{!! $item->title !!}</span>
@endif
@if($item->hasChildren())
<ul class="dropdown-menu">
@include(config('laravel-menu.views.bootstrap-items'),
array('items' => $item->children()))
</ul>
@endif
</li>
@if($item->divider)
<li{!! Lavary\Menu\Builder::attributes($item->divider) !!}></li>
@endif
@endforeach