# 2.2 Create middleware get.menu (ok)

{% file src="https://1957079826-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MCBeDUD-PK_RF-YgJRe%2Fuploads%2FszPKWzQDaMP0xWbX9tCB%2Ftestnet-start-menu.zip?alt=media&token=a2e93923-2467-4c0f-9f7d-eefb21e73af5" %}

### Phần 2: Gọi được menulist lấy phần tử cấp 1 target = link :)

<figure><img src="https://1957079826-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MCBeDUD-PK_RF-YgJRe%2Fuploads%2FxfcFVt151EbqQeNu4Loa%2Fimage.png?alt=media&#x26;token=9cb19918-527c-4ba6-87a5-46780a4500c8" alt=""><figcaption></figcaption></figure>

C:\xampp82\htdocs\testnet\app\Http\Middleware\GetMenu.php

```php
<?php
namespace App\Http\Middleware;
use Closure;
use App\Models\Menulist;
use App\Http\Menus\GetSidebarMenu;
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';
    $menus = new GetSidebarMenu();
    $menulists = Menulist::all();
    $result = array();
    foreach ($menulists as $menulist) {
      $result[$menulist->name] = $menus->get($role, $menulist->id);
    }
    view()->share('appMenus', $result);
    return $next($request);
  }
}

```

C:\xampp82\htdocs\testnet\app\Http\Menus\GetSidebarMenu.php

```php
<?php
/*
*   07.11.2019
*   MenusMenu.php
*/
namespace App\Http\Menus;
use App\MenuBuilder\MenuBuilder;
use Illuminate\Support\Facades\DB;
use App\Models\Menus;
use App\MenuBuilder\RenderFromDatabaseData;
class GetSidebarMenu implements MenuInterface
{
  private $mb; //menu builder
  private $menu;
  public function __construct()
  {
    $this->mb = new MenuBuilder();
  }
  private function getMenuFromDB($menuId, $menuName)
  {
    $this->menu = Menus::join('menu_role', 'menus.id', '=', 'menu_role.menus_id')
      ->select('menus.*')
      ->where('menus.menu_id', '=', $menuId)
      ->where('menu_role.role_name', '=', $menuName)
      ->orderBy('menus.sequence', 'asc')->get();
  }
  private function getGuestMenu($menuId)
  {
    $this->getMenuFromDB($menuId, 'guest');
  }
  private function getUserMenu($menuId)
  {
    $this->getMenuFromDB($menuId, 'user');
  }
  private function getAdminMenu($menuId)
  {
    $this->getMenuFromDB($menuId, 'admin');
  }
  public function get($role, $menuId = 2)
  {
    $this->getMenuFromDB($menuId, $role);
    $rfd = new RenderFromDatabaseData();
    return $rfd->render($this->menu);
  }
  public function getAll($menuId = 2)
  {
    $this->menu = Menus::select('menus.*')
      ->where('menus.menu_id', '=', $menuId)
      ->orderBy('menus.sequence', 'asc')->get();
    $rfd = new RenderFromDatabaseData;
    return $rfd->render($this->menu);
  }
}

```

C:\xampp82\htdocs\testnet\app\Http\Menus\MenuInterface.php

```php
<?php
/*
*   07.11.2019
*   GuestMenu.php
*/
namespace App\Http\Menus;
use App\MenuBuilder;
interface MenuInterface
{
  public function get(string $roles);
}

```

C:\xampp82\htdocs\testnet\app\MenuBuilder\RenderFromDatabaseData.php

```php
<?php
/*
*   08.11.2019
*   RenderFromDatabaseData
*/
namespace App\MenuBuilder;
use App\MenuBuilder\MenuBuilder;
class RenderFromDatabaseData
{
  private $mb; // MenuBuilder
  private $data;
  public function __construct()
  {
    $this->mb = new MenuBuilder();
  }
  private function addTitle($data)
  {
    $this->mb->addTitle($data['id'], $data['name'], false, 'coreui', $data['sequence']);
  }
  private function addLink($data)
  {
    if ($data['parent_id'] === NULL) {
      $this->mb->addLink($data['id'], $data['name'], $data['slug'], $data['icon'], 'coreui', $data['sequence']);
    }
  }
  private function dropdownLoop($id)
  {
    for ($i = 0; $i < count($this->data); $i++) {
      if ($this->data[$i]['parent_id'] == $id) {
        if ($this->data[$i]['target'] === 'dropdown') {
          $this->addDropdown($this->data[$i]);
        } elseif ($this->data[$i]['target'] === 'link') {
          $this->mb->addLink($this->data[$i]['id'], $this->data[$i]['name'], $this->data[$i]['target'], $this->data[$i]['icon'], 'coreui', $this->data[$i]['sequence']);
        } else {
          $this->addTitle($this->data[$i]);
        }
      }
    }
  }
  private function addDropdown($data)
  {
    $this->mb->beginDropdown($data['id'], $data['name'], $data['icon'], 'coreui', $data['sequence']);
    $this->dropdownLoop($data['id']);
    $this->mb->endDropdown();
  }
  private function mainLoop()
  {
    for ($i = 0; $i < count($this->data); $i++) {
      switch ($this->data[$i]['target']) {
        case 'title':
          $this->addTitle($this->data[$i]);
          break;
        case 'link':
          $this->addLink($this->data[$i]);
          break;
        case 'dropdown':
          if ($this->data[$i]['parent_id'] == null) {
            $this->addDropdown($this->data[$i]);
          }
          break;
      }
    }
  }
  /***
   * $data - array
   * return - array
   */
  public function render($data)
  {
    if (!empty($data)) {
      $this->data = $data;
      $this->mainLoop();
    }
    return $this->mb->getResult();
  }
}

```

C:\xampp82\htdocs\testnet\app\MenuBuilder\MenuBuilder.php

```php
<?php
/*
*   07.11.2019
*   MenuBuilder
*/
namespace App\MenuBuilder;
class MenuBuilder
{
  private $menu;
  private $dropdown;
  private $dropdownDeep;
  public function __construct()
  {
    $this->menu = array();
    $this->dropdown = false;
    $this->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;
        break;
      }
      $menu = &$menu['elements'][count($menu['elements']) - 1];
      $z++;
    }
    return $result;
  }
  private function addElementToMenuLastPosition($element, $offset = 0)
  {
    return $this->innerAddElementToMenuLastPosition($this->menu, $element, $offset);
  }
  private function addRegularLink($id, $name, $href, $icon, $iconType, $sequence = 0)
  {
    $hasIcon = ($icon === false || strlen($icon) === 0) ? false : true;
    if ($hasIcon) {
      array_push($this->menu, array(
        'id' => $id,
        'target' => 'link',
        'name' => $name,
        'slug' => $href,
        'hasIcon' => $hasIcon,
        'icon' => $icon,
        'iconType' => $iconType,
        'sequence' => $sequence,
      ));
    } else {
      array_push($this->menu, array(
        'id' => $id,
        'target' => 'link',
        'name' => $name,
        'slug' => $href,
        'hasIcon' => $hasIcon,
        'sequence' => $sequence,
      ));
    }
  }
  private function addDropdownLink($id, $name, $href, $icon, $iconType, $sequence = 0)
  {
    $num = count($this->menu);
    $hasIcon = ($icon === false || strlen($icon) === 0) ? false : true;
    if ($hasIcon) {
      $this->addElementToMenuLastPosition(array(
        'id' => $id,
        'target' => 'link',
        'name' => $name,
        'slug' => $href,
        'hasIcon' => $hasIcon,
        'icon' => $icon,
        'iconType' => $iconType,
        'sequence' => $sequence,
      ));
    } else {
      $this->addElementToMenuLastPosition(array(
        'id' => $id,
        'target' => 'link',
        'name' => $name,
        'slug' => $href,
        'hasIcon' => $hasIcon,
        'sequence' => $sequence,
      ));
    }
  }
  public function addLink($id, $name, $href, $icon = false, $iconType = 'coreui', $sequence = 0)
  {
    if ($this->dropdown === true) {
      $this->addDropdownLink($id, $name, $href, $icon, $iconType, $sequence);
    } else {
      $this->addRegularLink($id, $name, $href, $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, array(
        'id' => $id,
        'target' => 'title',
        'name' => $name,
        'hasIcon' => $hasIcon,
        'icon' => $icon,
        'iconType' => $iconType,
        'sequence' => $sequence,
      ));
    } else {
      array_push($this->menu, array(
        'id' => $id,
        'target' => 'title',
        'name' => $name,
        'hasIcon' => $hasIcon,
        'sequence' => $sequence,
      ));
    }
  }
  public function beginDropdown($id, $name, $icon = false, $iconType = 'coreui', $sequence = 0)
  {
    $this->dropdown = true;
    $this->dropdownDeep++;
    $hasIcon = ($icon === false || strlen($icon) === 0) ? false : true;
    if ($this->dropdownDeep === 1) {
      if ($hasIcon) {
        array_push($this->menu, array(
          'id' => $id,
          'target' => 'dropdown',
          'name' => $name,
          'hasIcon' => $hasIcon,
          'icon' => $icon,
          'iconType' => $iconType,
          'elements' => array(),
          'sequence' => $sequence,
        ));
      } else {
        array_push($this->menu, array(
          'id' => $id,
          'target' => 'dropdown',
          'name' => $name,
          'hasIcon' => $hasIcon,
          'elements' => array(),
          'sequence' => $sequence,
        ));
      }
    } else {
      if ($hasIcon) {
        $this->addElementToMenuLastPosition(array(
          'id' => $id,
          'target' => 'dropdown',
          'name' => $name,
          'hasIcon' => $hasIcon,
          'icon' => $icon,
          'iconType' => $iconType,
          'elements' => array(),
          'sequence' => $sequence,
        ), 1);
      } else {
        $this->addElementToMenuLastPosition(array(
          'id' => $id,
          'target' => 'dropdown',
          'name' => $name,
          'hasIcon' => $hasIcon,
          'elements' => array(),
          'sequence' => $sequence,
        ), 1);
      }
    }
  }
  public function endDropdown()
  {
    $this->dropdownDeep--;
    if ($this->dropdownDeep < 0) {
      $this->dropdownDeep = 0;
    }
    if ($this->dropdownDeep <= 0) {
      $this->dropdown = false;
    }
  }
  public function getResult()
  {
    return $this->menu;
  }
}

```

