2.0.1 Study addLink (ok)
Last updated
Last updated
C:\xampp82\htdocs\testvn\app\Http\Middleware\GetMenu.php
<?php
namespace App\Http\Middleware;
use App\Http\Menus\GetSidebarMenu;
use App\Models\Menulist;
use Closure;
use Illuminate\Http\Request;
class GetMenu
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure( \Illuminate\Http\Request ): ( \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse ) $next
* @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
*/
public function handle(Request $request, Closure $next)
{
$role = "admin";
$menulists = Menulist::all();
$menus = new GetSidebarMenu();
$result = [];
foreach ($menulists as $menulist) {
$result[$menulist->name] = $menus->get($role, $menulist->id);
}
view()->share('appMenus', $result);
return $next($request);
}
}
C:\xampp82\htdocs\testvn\app\Http\Menus\GetSidebarMenu.php
<?php
namespace App\Http\Menus;
use App\MenuBuilder\RenderFromDatabaseData;
use App\Models\Menus;
use Spatie\Permission\Models\Role;
class GetSidebarMenu implements MenuInterface
{
private $menu;
private function getMenuFromDB($role, $menuId)
{
$this->menu = Menus::join('menu_role', 'menus.id', '=', 'menu_role.menus_id')
->where('menus.menu_id', $menuId)
->where('menu_role.role_name', $role)
->orderBy('menus.sequence', 'asc')
->get();
}
private function getAdminMenu($menuId)
{
$this->getMenuFromDB('admin', $menuId);
}
private function getUserMenu($menuId)
{
$this->getMenuFromDB('user', $menuId);
}
private function getGuestMenu($menuId)
{
$this->getMenuFromDB('guest', $menuId);
}
public function get($role, $menuId = 2)
{
$this->getMenuFromDB($role, $menuId);
$rfd = new RenderFromDatabaseData();
return $rfd->render($this->menu);
}
}
C:\xampp82\htdocs\testvn\app\MenuBuilder\RenderFromDatabaseData.php
<?php
namespace App\MenuBuilder;
use App\MenuBuilder\MenuBuilder;
class RenderFromDatabaseData
{
private $mb;
private $data;
public function __construct()
{
$this->mb = new MenuBuilder();
}
private function addLink($data)
{
if ($data['parent_id'] == NULL) {
$this->mb->addLink($data['id'], $data['name'], $data['slug'], $data['icon'], $data['iconType'], $data['sequence']);
}
}
private function mainLoop()
{
for ($i = 0; $i < count($this->data); $i++) {
switch ($this->data[$i]['target']) {
case 'link':
$this->addLink($this->data[$i]);
break;
}
}
}
public function render($data) {
if(!empty($data)) {
$this->data = $data;
$this->mainLoop();
}
return $this->mb->getResult();
}
}
C:\xampp82\htdocs\testvn\app\MenuBuilder\MenuBuilder.php
<?php
namespace App\MenuBuilder;
class MenuBuilder
{
private $menu = [];
private $dropdown = false;
private $dropdownDeep = 0;
private function innerAddElementToMenuLastPosition(&$menu, $element, $offset)
{
$z = 1;
$result = false;
$menu = &$menu[count($menu) - 1];
while (is_array($menu)) {
if ($z == $this->dropdownDeep - $offset) {
array_push($menu['elements'], $element);
$result = true;
}
}
$menu = &$menu['elements'][count($menu['elements']) - 1];
$z++;
return $result;
}
private function addElementToMenuLastPosition($element, $offset = 0)
{
$this->innerAddElementToMenuLastPosition($this->menu, $element, $offset);
}
private function addRegularLink($id, $name, $slug, $icon, $iconType, $sequence = 0)
{
$hasIcon = ($icon === false || strlen($icon) === 0) ? false : true;
if ($hasIcon) {
array_push($this->menu, [
'id' => $id,
'target' => 'link',
'name' => $name,
'slug' => $slug,
'hasIcon' => $hasIcon,
'iconType' => $iconType,
'sequence' => $sequence
]);
} else {
array_push($this->menu, [
'id' => $id,
'target' => 'link',
'name' => $name,
'slug' => $slug,
'iconType' => $iconType,
'sequence' => $sequence
]);
}
}
public function addLink($id, $name, $slug, $icon = false, $iconType = 'coreui', $sequence = 0)
{
$this->addRegularLink($id, $name, $slug, $icon, $iconType, $sequence);
}
public function getResult()
{
return $this->menu;
}
}
C:\xampp82\htdocs\testvn\app\Http\Menus\MenuInterface.php
<?php
namespace App\Http\Menus;
interface MenuInterface {
public function get(string $roles);
}
C:\xampp82\htdocs\testvn\routes\web.php
<?php
use App\Http\Controllers\BannerController;
use App\Http\Controllers\HomeController;
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::group(['middleware' => 'get.menu'], function () {
Route::get('/', function () {
return view('dashboard.homepage');
});
});
C:\xampp82\htdocs\testvn\resources\views\dashboard\shared\nav-builder.blade.php
<ul class="sidebar-nav aaa2">
@if (isset($appMenus['sidebar menu']))
{{ dd($appMenus) }}
@endif
</ul>