Ajouter des pages personnalisées à la navigation d'administration de Bootstrap
Menu Customization Guide

Pour ajouter des liens vers des pages personnalisées dans la barre latérale du panneau d'administration Bootstrap, vous devez créer votre page, la mapper avec le routeur principal, puis l'ajouter à la barre latérale.


La structure de la barre latérale

La barre latérale est composée de catégories, de navigations et de liens. Les catégories sont les sections principales de la barre latérale, les navigateurs sont les sous-sections des catégories et les liens sont les liens proprement dits vers les pages.

Voici la structure :

Sidebar structure

Lien vers une ou plusieurs pages personnalisées dans la barre latérale du panneau d'administration Bootstrap

  1. Créez votre (vos) fichier(s) cible(s) dans admin/
    par exemple : admin/custom-page-one.php et admin/custom-page-two.php
  2. Créez de nouvelles règles de routage dans admin/index.php pour mener à votre (vos) nouvelle(s) page(s) :
    // 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. Ajoutez votre (vos) page(s) à la barre latérale

    Le code suivant crée une nouvelle catégorie nommée "Ma nouvelle catégorie", puis ajoute un objet Nav à la catégorie et enfin ajoute les liens à l'objet Nav.

    Les méthodes utilisées dans ce code sont les suivantes :

    • Sidebar::addCategory() - ajoute une nouvelle catégorie à la barre latérale - /class/bootstrap/sidebar/Sidebar.php
    • SidebarCategories::addNav() - ajoute un nouvel objet Nav à la catégorie - /class/bootstrap/sidebar/SidebarCategories.php
    • Nav::addLink() - ajoute un nouveau lien à l'objet Nav - /class/bootstrap/sidebar/Nav.php
    1. ouvrez admin\inc\sidebar.php
    2. Ajoutez le code suivant à la fin du fichier et personnalisez-le comme vous le souhaitez :
      
      // 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. Fait.

Page principale du tutoriel CRUD PHP