https://github.com/tattersoftware/codeigniter4-menus
Dynamic menus for CodeIgniter 4
https://github.com/tattersoftware/codeigniter4-menus
Last synced: 8 months ago
JSON representation
Dynamic menus for CodeIgniter 4
- Host: GitHub
- URL: https://github.com/tattersoftware/codeigniter4-menus
- Owner: tattersoftware
- License: mit
- Created: 2021-04-11T01:38:51.000Z (about 5 years ago)
- Default Branch: develop
- Last Pushed: 2024-01-18T13:45:05.000Z (over 2 years ago)
- Last Synced: 2025-02-01T00:26:40.170Z (over 1 year ago)
- Language: PHP
- Size: 101 KB
- Stars: 14
- Watchers: 4
- Forks: 4
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
- Security: SECURITY.md
Awesome Lists containing this project
README
# Tatter\Menus
Dynamic menus for CodeIgniter 4
[](https://github.com/tattersoftware/codeigniter4-menus/actions?query=workflow%3A%22PHPUnit%22)
[](https://github.com/tattersoftware/codeigniter4-menus/actions?query=workflow%3A%PHPStan%22)
[](https://coveralls.io/github/tattersoftware/codeigniter4-menus?branch=develop)
## Quick Start
1. Install with Composer: `> composer require tatter/menus`
2. Create your menus by extending `Tatter\Menus\Menu`
3. Add your menu aliases to `Config\Menus`
4. Apply `MenuFilter` to all routes that need menus
## Features
**Menus** provides dynamic menus across your application. **Menus** organizes and injects the
menu content, so you can focus on building.
## Installation
Install easily via Composer to take advantage of CodeIgniter 4's autoloading capabilities
and always be up-to-date:
* `> composer require tatter/menus`
Or, install manually by downloading the source files and adding the directory to
`app/Config/Autoload.php`.
## Configuration (optional)
The library's default behavior can be altered by extending its config file. Copy
**examples/Menus.php** to **app/Config/** and follow the instructions
in the comments. If no config file is found in app/Config the library will use its own.
## Usage
### Building
**Menus** is built on `Spatie\Menu` with all of its wonderful, dynamic and fluent functionality.
Use their documentation to craft your menus as simple or complex as you like:
* [Version 2](https://spatie.be/docs/menu/v2)
* [Version 3](https://spatie.be/docs/menu/v3) (PHP 8 only)
Create your menus by extending `Tatter\Menus\Menu`. You will notice in the source code that
`Menu` requires you to provide one method: `public function __toString(): string;`. You may use the
supplied `$builder` property to access the underlying `Spatie\Menu` to build your menu,
or provide your own HTML code or `view()` return. Some examples:
```
class MainMenu extends \Tatter\Menus\Menu
{
public function __toString(): string
{
return $this->builder
->link(site_url('/'), 'Home')
->link(site_url('/about'), 'About')
->html('
')
->link(site_url('/contact'), 'Contact')
->render();
}
}
class FruitMenu extends \Tatter\Menus\Menu
{
public function __toString(): string
{
return view('menus/fruit', ['active' => 'banana']);
}
}
```
Note: `$builder` is initialized with "set active" to the current URL. You may call `setActive()`
again to remove or change the active menu item. Due to a limitation in `Spatie\Menu` with mixing
relative and absolute URLs you must supply full URL values (e.g. with `site_url()`) to your
`Menu` if you want to use this default "active" URL.
### Deploying
Since `Menu` is `Stringable` it can be used in your view or layout files as is.
However, **Menus** also comes with a [Controller Filter](https://codeigniter4.github.io/CodeIgniter4/incoming/filters.html)
that you can use to inject menu content directly into your responses. First you need to create
an alias for each `Menu` class you would like to use. Create **app/Config/Menus.php** (or
start with a copy from the **examples** folder) and add your menu classes to the `$aliases`
array. For example:
```
class Menus extends \Tatter\Menus\Config\Menus
{
/**
* Menu class aliases.
*
* @var array
*/
public $aliases = [
'main' => \App\Menus\MainMenu::class,
'fruit' => \ShopModule\FruitMenu::class,
];
}
```
Once aliases are set up you can pass them as an argument to the `MenuFilter` for any route:
```
$routes->add('shop/(:any)', 'ShopModule\ShopController::show/$1', ['filter' => 'menus:fruit']);
```
Then in your view or layout put the placeholder token with the name of the alias target in
double curly braces:
```
{{main}}
Fruit Shop
{{fruit}}
...
```
Note that sometimes it is preferable to apply the filter in bulk using **app/Config/Filters.php**.
Unfortunately parameters are [not yet supported](https://github.com/codeigniter4/CodeIgniter4/issues/2078)
in `Config\Filters`, but you can work around this by creating your own parameter-specific Filter:
```
find($userId);
// Start with the default breadcrumbs
BreadcrumbsMenu::discover();
// Pop off the numeric last segment
BreadcrumbsMenu::pop();
// Replace it with the user's name
BreadcrumbsMenu::push(new Breadcrumb(current_url(), $user->name));
return view('users/show', ['user' => $user]);
}
}
```
... if you have the filter in place the rest is handled for you.