Skip to content

Instantly share code, notes, and snippets.

@omarkdev
Created October 19, 2016 17:57
Show Gist options
  • Save omarkdev/b438f208ce6c402c78e75f25788c981b to your computer and use it in GitHub Desktop.
Save omarkdev/b438f208ce6c402c78e75f25788c981b to your computer and use it in GitHub Desktop.
menu-composer
<?php
namespace App\Http\ViewComposers\Admin;
use Illuminate\View\View;
use Route;
class MenuComposer{
protected $routesPrefix = 'admin.';
protected $routesApi = ['destroy', 'create', 'store', 'show', 'update', 'edit'];
protected $routesMain = [
'welcome',
'informations',
'settings',
'users',
'products',
'categories',
'brands'
];
protected $routesSub = [
'products' => ['settings', 'images']
];
public function compose(View $view){
$route = str_replace($this->routesPrefix, '', Route::currentRouteName());
$dataRouteMain = NULL;
$dataRouteSub = NULL;
foreach ($this->routesMain as $main) {
if($main == $route or $route == $main.".main" or $route == $main.".index"){
$dataRouteMain = $main;
break;
}
if (strpos($route, $main) !== false) {
$sub = str_replace("{$main}.", "", $route);
if(!isset($this->routesSub[$main]) or !in_array($sub, $this->routesSub[$main])){
if(!in_array($sub, $this->routesApi))
continue;
}
$dataRouteMain = $main;
$dataRouteSub = $sub;
}
}
$dataMenu = [
'main' => $dataRouteMain,
'sub' => $dataRouteSub
];
$view->with('menu', $dataMenu);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment