A simple and effective Navigation to manage active states in navigation structures.
- Automatically marks parent navigation items as active when a child link is active.
- Easy to set up and integrate into existing projects.
- Flexible and extensible for complex navigation structures.
Install via Composer:
composer require feskol/php-navigation
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);
Iterating through Navigation links:
foreach ($navigation->getLinks() as $link){
echo $link->getTitle(); // "Company"
echo $link->isActive() ? 'active' : 'inactive'; // "active" - because the child is active
foreach($link->getChildren() as $subLink){
echo $link->getTitle(); // "About us"
echo $link->isActive() ? 'active' : 'inactive'; // "active"
}
}
There are often situations where you need additional data for your navigation, such as icons or images.
The best approach is to create your own class (e.g. MyCustomLink
) that extends the Link
class:
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;
}
}
Then use your MyCustomLink
class instead of the Link
class:
$myCustomLink = new MyCustomLink();
$myCustomLink->setTitle('Company')
->setHref('/company')
->setIcon('bi bi-user'); // For example, using Bootstrap-Icon classes
In Symfony, you can use the TranslatableMessage
class to hold translation infos which you can use in your frontend.
In Twig, apply the |trans
filter to translate the TranslatableMessage
.
Both the Navigation
and Link
classes accept any object implementing the \Stringable
interface in their
setTitle()
methods.
This allows you to pass objects like TranslatableMessage
or custom classes with a __toString()
method.
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);
And then in your Twig template:
{# @var \Feskol\Navigation\Navigation navigation #}
Translated Navigation Title: {{ navigation.title|trans }}
{% for link in navigation.links %}
Translated Link Title: {{ link.title|trans }}
{% endfor %}
We welcome contributions to this project! To ensure clarity and fairness for all contributors, we require that all contributors sign our Contributor License Agreement (CLA).
By signing the CLA, you confirm that:
- You grant us the perpetual, worldwide, non-exclusive, royalty-free, irrevocable right to use, modify, sublicense, and distribute your contribution as part of this project or any other project.
- You retain ownership of your contribution, but grant us the rights necessary to use it without restriction.
- Your contribution does not violate the rights of any third party.
Before submitting a pull request, please sign the CLA using the following link:
Sign the CLA
Contributions cannot be merged unless the CLA is signed.
Thank you for your contributions and for helping us build something great!
We're using phpunit.
Run tests:
php vendor/bin/phpunit
If you find this project helpful and would like to support my work:
Thank you for your support!