PHP code example of feskol / php-navigation

1. Go to this page and download the library: Download feskol/php-navigation library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

feskol / php-navigation example snippets


use Feskol\Navigation\Link;
use Feskol\Navigation\Navigation;

// create links
$navLink = new Link();
$navLink->setTitle('Company')
    ->setHref('/company');
    
$subNavLink = new Link();
$subNavLink->setTitle('About us')
    ->setHref('/company/about-us')
    ->setIsActive(true);
    
// add the $subNavLink as $navLinks Child
$navLink->addChild($subNavLink);


// To have all links in one place you can use the provided Navigation class:
$navigation = new Navigation();
$navigation->setTitle('MyNavigation');

// add the created $navLink to the Navigation
$navigation->addLink($navLink);

foreach ($navigation->getLinks() as $link){
    echo $link->getTitle(); // "Company"
    echo $link->hasActiveChildren(); // "true" - because the child is active
    
    foreach($link->getChildren() as $subLink){
        echo $link->getTitle(); // "About us"
        echo $link->isActive(); // "true"
    }
}

use Feskol\Navigation\Link;

$link = new Link();

// href
$link->setHref('/company/about-us');
$link->getHref(); // "/company/about-us"

// target
$link->setTarget(Link::TARGET_BLANK);
$link->getTarget(); // "_blank"

use Feskol\Navigation\Link;

class MyCustomLink extends Link
{
    private ?string $icon = null;
    
    public function getIcon(): ?string {
        return $this->icon;
    }
    
    public function setIcon(?string $icon): static {
        $this->icon = $icon;
        return $this;
    }
}

$link = new MyCustomLink();
$link->setTitle('Company')
    ->setHref('/company')
    ->setIcon('bi bi-user'); // For example, using Bootstrap-Icon classes

use Feskol\Navigation\Link;
use Feskol\Navigation\Navigation;
use Symfony\Component\Translation\TranslatableMessage;

$navigation = new Navigation();
$navigation->setTitle(new TranslatableMessage('MyNavigation', [], 'navigation'));

$navLink = new Link();
$navLink->setTitle(new TranslatableMessage(
    'Nav-Item for %customerName%',
    ['%customerName%' => $customer->getName()], // Dynamic translation parameters 
    'navigation' // Translation domain
));

$navigation->addLink($navLink);
bash
composer 
bash
php vendor/bin/phpunit