PHPackages                             tatter/menus - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. tatter/menus

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

tatter/menus
============

Dynamic menus for CodeIgniter 4

v1.0.2(4y ago)1713.8k↓50%4[1 PRs](https://github.com/tattersoftware/codeigniter4-menus/pulls)1MITPHPPHP ^7.4 || ^8.0

Since Apr 15Pushed 2y ago3 watchersCompare

[ Source](https://github.com/tattersoftware/codeigniter4-menus)[ Packagist](https://packagist.org/packages/tatter/menus)[ Docs](https://github.com/tattersoftware/codeigniter4-menus)[ Fund](https://paypal.me/tatter)[ GitHub Sponsors](https://github.com/tattersoftware)[ RSS](/packages/tatter-menus/feed)WikiDiscussions develop Synced 1mo ago

READMEChangelog (7)Dependencies (3)Versions (12)Used By (1)

Tatter\\Menus
=============

[](#tattermenus)

Dynamic menus for CodeIgniter 4

[![](https://github.com/tattersoftware/codeigniter4-menus/workflows/PHPUnit/badge.svg)](https://github.com/tattersoftware/codeigniter4-menus/actions?query=workflow%3A%22PHPUnit%22)[![](https://github.com/tattersoftware/codeigniter4-menus/workflows/PHPStan/badge.svg)](https://github.com/tattersoftware/codeigniter4-menus/actions?query=workflow%3A%PHPStan%22)[![Coverage Status](https://camo.githubusercontent.com/5aa3183a8aa41fae28c344860e2d580c831f921ef55a0d412ed3a163f332d27f/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f746174746572736f6674776172652f636f646569676e69746572342d6d656e75732f62616467652e7376673f6272616e63683d646576656c6f70)](https://coveralls.io/github/tattersoftware/codeigniter4-menus?branch=develop)

Quick Start
-----------

[](#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
--------

[](#features)

**Menus** provides dynamic menus across your application. **Menus** organizes and injects the menu content, so you can focus on building.

Installation
------------

[](#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)
------------------------

[](#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
-----

[](#usage)

### Building

[](#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

[](#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:

```
