Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/icings/menu

A KnpMenu seasoned menu plugin for CakePHP.
https://github.com/icings/menu

cakephp-plugin knpmenu menu php

Last synced: 3 months ago
JSON representation

A KnpMenu seasoned menu plugin for CakePHP.

Awesome Lists containing this project

README

        

# Menu

[![Build Status][ico-build]][link-build]
[![Coverage Status][ico-coverage]][link-coverage]
[![Latest Version][ico-version]][link-version]
[![Software License][ico-license]][link-license]

[ico-build]: https://img.shields.io/github/actions/workflow/status/icings/menu/ci.yml?branch=5.x&style=flat-square
[ico-coverage]: https://img.shields.io/codecov/c/github/icings/menu/5.x.svg?style=flat-square
[ico-version]: https://img.shields.io/packagist/v/icings/menu.svg?style=flat-square&label=latest
[ico-license]: https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square

[link-build]: https://github.com/icings/menu/actions/workflows/ci.yml?query=branch%3A5.x
[link-coverage]: https://codecov.io/github/icings/menu/tree/5.x
[link-version]: https://packagist.org/packages/icings/menu
[link-license]: LICENSE.txt

A [KnpMenu](https://github.com/KnpLabs/KnpMenu) seasoned plugin that assists with creating menus for your
[CakePHP](https://cakephp.org) applications.

## Requirements

* CakePHP 5.0+ (use the [4.x branch](https://github.com/icings/menu/tree/4.x) of this plugin if you need CakePHP 4
compatibility)
* KnpMenu 3.3+

## Installation

1. Use [Composer](http://getcomposer.org) to add the menu plugin to your project:

```bash
$ composer require icings/menu
```

2. Make sure that you are loading the plugin in your bootstrap, either run:

```bash
$ bin/cake plugin load Icings/Menu
```

or add the following call to your `Application` class' `bootstrap()` method in the `src/Application.php` file:

```php
$this->addPlugin('Icings/Menu');
```

3. Load the helper in your `AppView` class' `initialize()` method, located in the `src/View/AppView.php` file:

```php
$this->loadHelper('Icings/Menu.Menu');
```

## Usage

Detailed usage documentation can be found in the [docs](docs/index.md) folder. For those that are familiar with CakePHP
and KnpMenu, here's two examples for a quick start.

### Via the Menu helper

Build and render the menu via the helpers `create()` and `render()` methods:

```php
$menu = $this->Menu->create('main');
$menu->addChild('Home', ['uri' => ['controller' => 'Pages', 'action' => 'display', 'home']]);
$menu->addChild('About', ['uri' => ['controller' => 'Pages', 'action' => 'display', 'about']]);

$menu->addChild('Services', ['uri' => '#']);
$menu['Services']->addChild('Research', ['uri' => ['controller' => 'Pages', 'action' => 'display', 'research']]);
$menu['Services']->addChild('Security', ['uri' => ['controller' => 'Pages', 'action' => 'display', 'security']]);

$menu->addChild('Contact', ['uri' => ['controller' => 'Pages', 'action' => 'display', 'contact']]);

echo $this->Menu->render();
```

In the default setup, this would generate the following HTML:

```html


```

### Using the library directly

Aside from using the menu helper and its various configuration possibilities, it's also possible to manually utilize
the library provided by this plugin, optionally combining things with the KnpMenu library:

```php
use Icings\Menu\Integration\PerItemVotersExtension;
use Icings\Menu\Integration\RoutingExtension;
use Icings\Menu\Integration\TemplaterExtension;
use Icings\Menu\Matcher\Matcher;
use Icings\Menu\Matcher\Voter\UrlVoter;
use Icings\Menu\MenuFactory;
use Icings\Menu\Renderer\StringTemplateRenderer;

$factory = new MenuFactory();
$factory->addExtension(new RoutingExtension());
$factory->addExtension(new PerItemVotersExtension());
$factory->addExtension(new TemplaterExtension());

$menu = $factory->createItem('main');
$menu->addChild('Home', ['uri' => ['controller' => 'Pages', 'action' => 'display', 'home']]);
$menu->addChild('About', ['uri' => ['controller' => 'Pages', 'action' => 'display', 'about']]);
$menu->addChild('Services', ['uri' => '#']);
$menu['Services']->addChild('Research', ['uri' => ['controller' => 'Pages', 'action' => 'display', 'research']]);
$menu['Services']->addChild('Security', ['uri' => ['controller' => 'Pages', 'action' => 'display', 'security']]);
$menu->addChild('Contact', ['uri' => ['controller' => 'Pages', 'action' => 'display', 'contact']]);

$matcher = new Matcher();
$matcher->addVoter(new UrlVoter($this->request));

$renderer = new StringTemplateRenderer($matcher);
echo $renderer->render($menu);
```

## Issues

Please use the [issue tracker](https://github.com/icings/menu/issues) to report problems.