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. */namespaceHttp\Middlewares;useMaplePHP\Handler\Interfaces\MiddlewareInterface;useMaplePHP\Http\Interfaces\ResponseInterface;useMaplePHP\Http\Interfaces\RequestInterface;useMaplePHP\Foundation\Http\Provider;useMaplePHP\Foundation\Nav\Middleware\Navigation;useMaplePHP\Foundation\Nav\Navbar;classNavextendsNavigationimplementsMiddlewareInterface{protected $provider;protected $nav;publicfunction__construct(Provider $provider,Navbar $nav) {$this->provider = $provider;$this->nav = $nav; }/** * Before controllers * @paramResponseInterface $response * @paramRequestInterface $request * @returnResponseInterface|void */publicfunctionbefore(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 navigationreturnparent::before($response, $request); }/** * After controllers * @paramResponseInterface $response * @paramRequestInterface $request * @returnvoid */publicfunctionafter(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.