2.0.3 Study addTitle (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\resources\views\dashboard\shared\nav-builder.blade.php
<ul class="sidebar-nav aaa2">
{{ dd($appMenus) }}
@if (isset($appMenus['sidebar menu']))
@endif
</ul>
C:\xampp82\htdocs\testvn\app\Http\Menus\GetSidebarMenu.php
<?php
namespace App\Http\Menus;
use App\MenuBuilder\MenuBuilder;
use App\MenuBuilder\RenderFromDatabaseData;
use App\Models\Menus;
class GetSidebarMenu implements MenuInterface
{
private $menu;
private function getMenuFromDB($role, $menuId)
{
$this->menu = Menus::join('menu_role', 'menus.id', '=', 'menu_role.menus_id')
->select('menus.*')
->where('menus.menu_id', '=', $menuId)
->where('menu_role.role_name', '=', $role)
->orderBy('menus.sequence', 'asc')->get();
}
public function get($role, $menuId = 2)
{
$this->getMenuFromDB($role, $menuId);
$rfd = new RenderFromDatabaseData();
return $rfd->render($this->menu);
}
}
C:\xampp82\htdocs\testvn\app\Http\Menus\MenuInterface.php
<?php
namespace App\Http\Menus;
interface MenuInterface {
public function get(string $role);
}
C:\xampp82\htdocs\testvn\database\seeders\MenusTableSeeder.php
<?php
namespace Database\Seeders;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
class MenusTableSeeder extends Seeder
{
private $joinData = [];
private $dropdown = false;
private $menuId = null;
private $dropdownId = [];
private $sequence = 1;
public function join($roles, $menuId)
{
$roles = explode(',', $roles);
foreach ($roles as $role) {
array_push($this->joinData, ['role_name' => $role, 'menus_id' => $menuId]);
}
}
public function joinAllData()
{
foreach ($this->joinData as $data) {
DB::table('menu_role')->insert($data);
}
}
public function insertLink($roles, $name, $slug, $icon = null)
{
if ($this->dropdown == false) {
DB::table('menus')->insert([
'name' => $name,
'icon' => $icon,
'slug' => $slug,
'target' => 'link',
'menu_id' => $this->menuId,
'parent_id' => null,
'sequence' => $this->sequence
]);
} else {
DB::table('menus')->insert([
'name' => $name,
'icon' => $icon,
'slug' => $slug,
'target' => 'link',
'menu_id' => $this->menuId,
'parent_id' => $this->dropdownId[count($this->dropdownId) - 1],
'sequence' => $this->sequence
]);
}
$this->sequence++;
$lastId = DB::getPdo()->lastInsertId();
$this->join($roles, $lastId);
return $lastId;
}
public function insertTitle($roles, $name)
{
DB::table('menus')->insert([
'name' => $name,
'icon' => null,
'slug' => null,
'target' => 'text',
'menu_id' => $this->menuId,
'parent_id' => null,
'sequence' => $this->sequence
]);
$this->sequence++;
$lastId = DB::getPdo()->lastInsertId();
$this->join($roles, $lastId);
return $lastId;
}
public function beginDropdown($roles, $name, $icon = null)
{
if (count($this->dropdownId)) {
$parentId = $this->dropdownId[count($this->dropdownId) - 1];
} else {
$parentId = null;
}
DB::table('menus')->insert([
'name' => $name,
'icon' => $icon,
'slug' => null,
'target' => 'dropdown',
'menu_id' => $this->menuId,
'parent_id' => $parentId,
'sequence' => $this->sequence
]);
$this->sequence++;
$lastId = DB::getPdo()->lastInsertId();
array_push($this->dropdownId, $lastId);
$this->dropdown = true;
$this->join($roles, $lastId);
return $lastId;
}
public function endDropdown()
{
$this->dropdown = false;
array_pop($this->dropdownId);
}
/**
* Run the database seeds.
*/
public function run(): void
{
DB::table('menulist')->insert([
'name' => 'sidebar menu'
]);
$this->menuId = DB::getPdo()->lastInsertId(); //set menuId
$this->insertLink('guest,user,admin', 'Dashboard', '/', 'cil-speedometer');
$this->beginDropdown('admin', 'Settings', 'cil-calculator');
$this->insertLink('admin', 'Notes', '/notes');
$this->insertLink('admin', 'Users', '/users');
$this->insertLink('admin', 'Edit menu', '/menu/menu');
$this->insertLink('admin', 'Edit menu elements', '/menu/element');
$this->insertLink('admin', 'Edit roles', '/roles');
$this->insertLink('admin', 'Media', '/media');
$this->insertLink('admin', 'BREAD', '/bread');
$this->insertLink('admin', 'Email', '/mail');
$this->endDropdown();
$this->insertLink('guest', 'Login', '/login', 'cil-account-logout');
$this->insertLink('guest', 'Register', '/register', 'cil-account-logout');
$this->insertTitle('user,admin', 'Theme');
$this->insertLink('user,admin', 'Colors', '/colors', 'cil-drop1');
$this->insertLink('user,admin', 'Typography', '/typography', 'cil-pencil');
$this->beginDropdown('user,admin', 'Base', 'cil-puzzle');
$this->insertLink('user,admin', 'Breadcrumb', '/base/breadcrumb');
$this->insertLink('user,admin', 'Cards', '/base/cards');
$this->insertLink('user,admin', 'Carousel', '/base/carousel');
$this->insertLink('user,admin', 'Collapse', '/base/collapse');
$this->insertLink('user,admin', 'Forms', '/base/forms');
$this->insertLink('user,admin', 'Jumbotron', '/base/jumbotron');
$this->insertLink('user,admin', 'List group', '/base/list-group');
$this->insertLink('user,admin', 'Navs', '/base/navs');
$this->insertLink('user,admin', 'Pagination', '/base/pagination');
$this->insertLink('user,admin', 'Popovers', '/base/popovers');
$this->insertLink('user,admin', 'Progress', '/base/progress');
$this->insertLink('user,admin', 'Scrollspy', '/base/scrollspy');
$this->insertLink('user,admin', 'Switches', '/base/switches');
$this->insertLink('user,admin', 'Tables', '/base/tables');
$this->insertLink('user,admin', 'Tabs', '/base/tabs');
$this->insertLink('user,admin', 'Tooltips', '/base/tooltips');
$this->endDropdown();
$this->beginDropdown('user,admin', 'Buttons', 'cil-cursor');
$this->insertLink('user,admin', 'Buttons', '/buttons/buttons');
$this->insertLink('user,admin', 'Buttons Group', '/buttons/button-group');
$this->insertLink('user,admin', 'Dropdowns', '/buttons/dropdowns');
$this->insertLink('user,admin', 'Brand Buttons', '/buttons/brand-buttons');
$this->endDropdown();
$this->insertLink('user,admin', 'Charts', '/charts', 'cil-chart-pie');
$this->beginDropdown('user,admin', 'Icons', 'cil-star');
$this->insertLink('user,admin', 'CoreUI Icons', '/icon/coreui-icons');
$this->insertLink('user,admin', 'Flags', '/icon/flags');
$this->insertLink('user,admin', 'Brands', '/icon/brands');
$this->endDropdown();
$this->beginDropdown('user,admin', 'Notifications', 'cil-bell');
$this->insertLink('user,admin', 'Alerts', '/notifications/alerts');
$this->insertLink('user,admin', 'Badge', '/notifications/badge');
$this->insertLink('user,admin', 'Modals', '/notifications/modals');
$this->endDropdown();
$this->insertLink('user,admin', 'Widgets', '/widgets', 'cil-calculator');
$this->insertTitle('user,admin', 'Extras');
$this->beginDropdown('user,admin', 'Pages', 'cil-star');
$this->insertLink('user,admin', 'Login', '/login');
$this->insertLink('user,admin', 'Register', '/register');
$this->insertLink('user,admin', 'Error 404', '/404');
$this->insertLink('user,admin', 'Error 500', '/500');
$this->endDropdown();
$this->insertLink('guest,user,admin', 'Download CoreUI', 'https://coreui.io', 'cil-cloud-download');
$this->insertLink('guest,user,admin', 'Try CoreUI PRO', 'https://coreui.io/pro/', 'cil-layers');
/* Create top menu */
DB::table('menulist')->insert([
'name' => 'top menu'
]);
$this->menuId = DB::getPdo()->lastInsertId(); //set menuId
$this->beginDropdown('guest,user,admin', 'Pages');
$id = $this->insertLink('guest,user,admin', 'Dashboard', '/');
$id = $this->insertLink('user,admin', 'Notes', '/notes');
$id = $this->insertLink('admin', 'Users', '/users');
$this->endDropdown();
$id = $this->beginDropdown('admin', 'Settings');
$id = $this->insertLink('admin', 'Edit menu', '/menu/menu');
$id = $this->insertLink('admin', 'Edit menu elements', '/menu/element');
$id = $this->insertLink('admin', 'Edit roles', '/roles');
$id = $this->insertLink('admin', 'Media', '/media');
$id = $this->insertLink('admin', 'BREAD', '/bread');
$this->endDropdown();
$this->joinAllData();
}
}
C:\xampp82\htdocs\testvn\app\MenuBuilder\MenuBuilder.php
<?php
namespace App\MenuBuilder;
class MenuBuilder
{
private $menu = [];
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 addTitle($id, $name, $icon = false, $iconType = 'coreui', $sequence = 0)
{
$hasIcon = ($icon === false || strlen($icon) === 0) ? false : true;
if ($hasIcon) {
array_push($this->menu, [
'id' => $id,
'target' => 'title',
'name' => $name,
'hasIcon' => $hasIcon,
'iconType' => $iconType,
'sequence' => $sequence
]);
} else {
array_push($this->menu, [
'id' => $id,
'target' => 'title',
'name' => $name,
'iconType' => $iconType,
'sequence' => $sequence
]);
}
}
public function getResult()
{
return $this->menu;
}
}
C:\xampp82\htdocs\testvn\app\MenuBuilder\RenderFromDatabaseData.php
<?php
namespace App\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'], false, 'coreui', $data['sequence']);
}
}
private function addTitle($data)
{
$this->mb->addTitle($data['id'], $data['name'], false, 'coreui', $data['sequence']);
}
private function loopMain()
{
for ($i = 0; $i < count($this->data); $i++) {
switch ($this->data[$i]['target']) {
case 'link':
$this->addLink($this->data[$i]);
break;
case 'text':
$this->addTitle($this->data[$i]);
break;
}
}
}
public function render($data)
{
if (!empty($data)) {
$this->data = $data;
$this->loopMain();
}
return $this->mb->getResult();
}
}
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');
});
});