Añadir páginas personalizadas a la navegación de administración de Bootstrap
Guía de personalización de menús

Para añadir enlaces a páginas personalizadas en la barra lateral del panel de administración de Bootstrap tiene que crear su página, mapearla con el enrutador principal y luego añadirla a la barra lateral.


La estructura de la barra lateral

La barra lateral se compone de categorías, navs y enlaces. Las categorías son las secciones principales de la barra lateral, las navs son las subsecciones de las categorías y los enlaces son los enlaces reales a las páginas.

He aquí la estructura:

Sidebar structure

Enlace a la(s) página(s) personalizada(s) en la barra lateral del panel de administración de Bootstrap

  1. Cree su(s) archivo(s) de destino en admin/
    es decir: admin/custom-page-one .php y admin/custom-page-two.php
  2. Cree una(s) nueva(s) regla(s) de enrutamiento en admin/index. php para dirigir a su(s) nueva(s) página(s). p. ej:
    // 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. Añada su(s) página(s) a la barra lateral

    El siguiente código crea una nueva categoría denominada "Mi nueva categoría", después añade un objeto Nav a la categoría y, por último, añade los enlaces al objeto Nav.

    Los métodos utilizados en este código son los siguientes:

    • Sidebar::addCategory() - añade una nueva categoría a la barra lateral - /class/bootstrap/sidebar/Sidebar.php
    • SidebarCategories::addNav() - añade un nuevo objeto Nav a la categoría - /class/bootstrap/sidebar/SidebarCategories.php
    • Nav::addLink() - añade un nuevo enlace al objeto Nav - /class/bootstrap/sidebar/Nav.php
    1. abra admin\inc\sidebar.php
    2. Añada el siguiente código al final del archivo y personalícelo como desee:
      
      // 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. Hecho.

Página principal del tutorial PHP CRUD