Aggiunta di pagine personalizzate alla navigazione amministrativa Bootstrap
Guida alla personalizzazione del menu

Per aggiungere collegamenti a pagine personalizzate nella barra laterale del pannello amministrativo Bootstrap, deve creare la sua pagina, mapparla con il router principale e poi aggiungerla alla barra laterale.


La struttura della barra laterale

La barra laterale è composta da categorie, nav e link. Le categorie sono le sezioni principali della barra laterale, le nav sono le sottosezioni delle categorie e i link sono i collegamenti effettivi alle pagine.

Ecco la struttura:

Sidebar structure

Link alla/e pagina/e personalizzata/e nella barra laterale del Pannello Admin di Bootstrap

  1. Crei il/i file di destinazione in admin/
    cioè: admin/custom-page-one.php e admin/custom-page-two.php
  2. Crei una o più nuove regole del router in admin/index.php per condurre alle sue nuove pagine:
    // add your custom page(s) to the router
    // the first parameter is the method (GET, POST, PUT, DELETE)
    // the second parameter is the path to the page
    // the third parameter is the name given to the route (any arbitrary name. It must be unique, without spaces, without special characters)
    $router->map('GET', '/custom-page-one', 'custom-page-one.php', 'custom-page-one');
    $router->map('GET', '/custom-page-two', 'custom-page-two.php', 'custom-page-two');
  3. Aggiunga la sua pagina (o le sue pagine) alla barra laterale

    Il codice seguente crea una nuova categoria denominata "La mia nuova categoria", quindi aggiunge un oggetto Nav alla categoria e infine aggiunge i link all'oggetto Nav.

    I metodi utilizzati in questo codice sono i seguenti:

    • Sidebar::addCategory() - aggiunge una nuova categoria alla barra laterale - /class/bootstrap/sidebar/Sidebar.php
    • SidebarCategories::addNav() - aggiunge un nuovo oggetto Nav alla categoria - /class/bootstrap/sidebar/SidebarCategories.php
    • Nav::addLink() - aggiunge un nuovo link all'oggetto Nav - /class/bootstrap/sidebar/Nav.php
    1. apra adminincsidebar.php
    2. Aggiunga il seguente codice alla fine del file e lo personalizzi come desidera:
      
      // create an array with the pages that we'll add to the "My New Category" category
      // using the route names we gave in index.php => $router->map()
      $category_pages = array('custom-page-one', 'custom-page-two');
      
      /* ===================================
      =              Category              =
      ==================================== */
      
      $is_category_collapsed = true;
      
      // test if the active (current) page is in the $category_pages array using $match from the router
      // and expand the category accordingly
      if (isset($match['name']) && in_array($match['name'], $category_pages)) {
          $is_category_collapsed = false;
      }
      
      // add the category
      $sidebar->addCategory('new-category', 'New Category', '', '', true, $is_category_collapsed);
      
      /* ===================================
      =                 Nav                =
      ==================================== */
      
      // create a nav object inside the category. The nav object will contain the links
      // add nav into category - the 'newCategory' object is the sidebar's newly created category.
      // its name ('newCategory') is the lower-camelcase version of 'new-category'
      $sidebar->newCategory->addNav('new-nav', 'navbar-nav flex-column py-0');
      
      /* ===================================
      =                Links               =
      ==================================== */
      
      // first link
      
      $active = false;
      
      // check if the page is active and set the active variable to true
      if (isset($match['name']) && $match['name'] == 'custom-page-one') {
          $active = true;
      }
      
      // add the first link to the nav object
      $sidebar->newCategory->newNav->addLink(ADMIN_URL . 'custom-page-one', 'Custom page one', 'fas fa-circle-info', $active, 'class=nav-item', 'class=nav-link d-flex align-items-center');
      
      // second link
      
      $active = false;
      
      // check if the page is active and set the active variable to true
      if (isset($match['name']) && $match['name'] == 'custom-page-two') {
          $active = true;
      }
      
      // add the second link to the nav object
      $sidebar->newCategory->newNav->addLink(ADMIN_URL . 'custom-page-two', 'Custom page two', 'fas fa-users', $active, 'class=nav-item', 'class=nav-link d-flex align-items-center');
      
  4. Fatto.

Pagina principale del tutorial PHP CRUD