MaplePHP
  • MaplePHP - Layered structure MVC PHP framework
  • Guide
    • Get started
    • Installation
    • Service Provider
    • Dependency injector
    • Controller
    • Middlewares
    • Routers
  • Navigation
    • Simple navigation
    • Dynamic navigation
  • Template engine
    • Getting started
    • Templates
      • Index
      • Views
      • Partials
    • Content (DTO)
      • String
      • Number
      • Array
      • DateTime
      • Encode
      • Dom
  • Form builder
    • Form builder
  • Database
    • MySQL
  • Service providers
    • url
    • encode
    • dir
    • DB
    • responder
  • Installations
    • Authorization & login
  • Frontend template
    • Responder
    • Views & components
    • Modals
    • Redirects
  • Access
    • MaplePHP with Docker
  • Libraries
    • Container
    • MySQL queries
    • Validation
    • Cache
    • Logger
    • Request
    • Response
  • What is?
    • Dependency injector
    • Controller
    • Middleware
    • Router
Powered by GitBook
On this page

Was this helpful?

  1. Navigation

Dynamic navigation

This is just a partial example of the previous guide section "Simple navigation," so make sure you read that before continuing with this.

Create a new PHP middleware file in the directory "/Http/Middlewares/Nav.php" and copy-paste the boilerplate content below.

<?php
/**
 * Create a dynamic and custom navigation
 * 
 * You can also create a navigation model and extend to "MaplePHP\Foundation\Nav\Navbar"
 * the overwrite the methods in it to create a custom navbar
 * Take a look at the file Foundation/Nav/Navbar.php in your vendor directory.
 */

namespace Http\Middlewares;

use MaplePHP\Handler\Interfaces\MiddlewareInterface;
use MaplePHP\Http\Interfaces\ResponseInterface;
use MaplePHP\Http\Interfaces\RequestInterface;
use MaplePHP\Foundation\Http\Provider;
use MaplePHP\Foundation\Nav\Middleware\Navigation;
use MaplePHP\Foundation\Nav\Navbar;

class Nav extends Navigation implements MiddlewareInterface
{
    protected $provider;
    protected $nav;

    public function __construct(Provider $provider, Navbar $nav)
    {
        $this->provider = $provider;
        $this->nav = $nav;
    }

    /**
     * Before controllers
     * @param  ResponseInterface $response
     * @param  RequestInterface  $request
     * @return ResponseInterface|void
     */
    public function before(ResponseInterface $response, RequestInterface $request)
    {

        // You can use this middelware to create an dynamic navigation
        // The id is not required, but will create it´s own id with increment, starting from 1 if not filled in. 
        // The id is used to select parent!
        $this->nav->add("main", [
            "id" => 1,
            "name" => "Start",
            "slug" => "",
            "parent" => 0,
            "title" => "Meta title start",
            "description" => "Meta description start"
            
        ])->add("main", [
            "id" => 2,
            "name" => "Contact",
            "slug" => "contact",
            "parent" => 0,
            "title" => "Meta title contact",
            "description" => "Meta description contact"
        ]);

        // Will build the navigation
        return parent::before($response, $request);
    }

    /**
     * After controllers
     * @param  ResponseInterface $response
     * @param  RequestInterface  $request
     * @return void
     */
    public function after(ResponseInterface $response, RequestInterface $request)
    {
    }
}

Adding items

You add items with the add method. The above example manually adds items to your navigation just as a example, this should tho dynamically be propagated from your database.

$this->nav->add(string $nav, array $item): self

You add items with the add method.

  • nav: The nav parameter is the expected navigation you want to bind the navigation item to.

  • item: This is the navigation item.

Then add the middleware to your router to show it.

Http\Middlewares\Nav::class
PreviousSimple navigationNextGetting started

Last updated 1 year ago

Was this helpful?