dotkernel / dot-navigation
Dotkernel navigation component
Installs: 22 718
Dependents: 3
Suggesters: 0
Security: 0
Stars: 6
Watchers: 1
Forks: 3
Open Issues: 3
pkg:composer/dotkernel/dot-navigation
Requires
- php: ~8.2.0 || ~8.3.0 || ~8.4.0 || ~8.5.0
- dotkernel/dot-authorization: ^3.4.1
- dotkernel/dot-helpers: ^3.7.0
- laminas/laminas-escaper: ^2.13.0
- laminas/laminas-servicemanager: ^4.0
- mezzio/mezzio-template: ^2.9.0
- psr/http-message: ^1.0 || ^2.0
- psr/http-server-middleware: ^1.0.2
Requires (Dev)
- laminas/laminas-coding-standard: ^3.0.0
- phpstan/phpstan: ^2.1
- phpstan/phpstan-phpunit: ^2.0
- phpunit/phpunit: ^10.4.2
README
dot-navigation is Dotkernel's component that allows you to easily define and parse menus inside templates, using a configuration-based approach.
Documentation
Documentation is available at: https://docs.dotkernel.org/dot-navigation/.
Badges
Installation
Install dotkernel/dot-navigation by executing the following Composer command:
composer require dotkernel/dot-navigation
Merge ConfigProvider to your application's configuration.
The package uses dot-helpers package, please merge dot-helpers
ConfigProviderto your application's configuration also, if it's not merged already!
Register NavigationMiddleware in your middleware pipe between the routing and the dispatching middleware.
Configuration
Locate dot-navigation's distributable config file vendor/dotkernel/dot-navigation/config/autoload/navigation.global.php.dist and duplicate it in your project as config/autoload/navigation.global.php
Components
A menu, or navigation container, is a class implementing the RecursiveIterator interface.
It has a hierarchical structure, with nodes called pages (see the Page class) that may have children. It is basically a tree
A Page extends the NavigationContainer class. The NavigationContainer is the top most node which represents the entire menu. The children of this node are Page instances that define each navigation item.
A page has a reference to its parent and can have options and attributes. There is no limitation on what is accepted as options or attributes.
Options can be any piece of information that describes a page. Some predefined options exist in order for the navigation module to work seamlessly with other dot modules.
Attributes are key value pairs that define the menu item. They are usually inserted as HTML attributes when parsing the menu, but of course, this is implementation-specific.
A NavigationService class, is the service that handles all defined menu containers.
It can fetch the container from its provider, check if a page is active or not, and get the page's generated URI.
Container providers
Each menu can be created from different sources. The responsibility of creating a menu container from the source falls on a container provider.
Each provider must implement the interface ProviderInterface and be registered in the ProviderPluginManager.
We offer just one provider for now, ArrayProvider, that is able to fetch and create a menu container from a php array that is defined in the configuration file.
NavigationRenderer
Used to render the navigation container in a displayable format. It can render a simple HTML unordered list or use a partial template to render the menu in a template engine.
The partial method is usually the more flexible one, custom rules can be defined and checked in the template.
If you are using Twig, there is already a Twig extension provided in package dot-twigrenderer that you can use to easily parse the menus inside your templates
When using the partial method, the template will receive as parameters the container, the navigation service, and any extra parameters set by the developer.
Navigation containers are referred to, when parsed, by their name, as defined in the configuration file.
Required page options and attributes
The following are options that each page should define in the configuration
- label- the text of the menu item
- routeor- uri- defines the route or link the menu item will have
- permission- can be used optionally if authorization service is present, to omit menu items that are not authorized to visit.