zentlix/knp-menu

此包为基于Spiral框架的项目提供面向对象的菜单

v1.1.0 2023-05-14 10:27 UTC

This package is auto-updated.

Last update: 2024-09-11 06:41:48 UTC


README

PHP Version Require Latest Stable Version phpunit psalm Codecov Total Downloads type-coverage psalm-level

此包为基于Spiral框架的项目提供面向对象的菜单。

要求

确保您的服务器已配置以下PHP版本和扩展

  • PHP 8.1+
  • Spiral框架 3.5+

安装

您可以通过composer安装此包

composer require zentlix/knp-menu

要启用此包,只需将 Spiral\KnpMenu\Bootloader\KnpMenuBootloader 添加到您的应用程序类中的bootloaders列表即可。

protected const LOAD = [
    // ...
    \Spiral\KnpMenu\Bootloader\KnpMenuBootloader::class,
];

注意 如果您正在使用 spiral-packages/discoverer,则无需自行注册bootloader。

配置

此包的配置文件应位于 app/config/knp-menu.php。在此文件中,您可以配置所有菜单的默认 templatetemplate_options。您还可以注册 menus

例如,配置文件可能看起来像这样。

use App\Menu\Sidebar;
use App\Menu\TopBar;
use App\Menu\OtherMenu;
use Spiral\Core\Container\Autowire;

return [
    /**
     * -------------------------------------------------------------------------
     *  Default template for all menus
     * -------------------------------------------------------------------------
     */
    'template' => 'menu.twig',

    /**
     * -------------------------------------------------------------------------
     *  Template options for all menus
     * -------------------------------------------------------------------------
     */
    'template_options' => [
        'foo' => 'bar'
    ],

    /**
     * -------------------------------------------------------------------------
     *  Application menus list
     * -------------------------------------------------------------------------
     *
     *  As a key, you can specify the name of the menu, using this key you can get the menu.
     *  If the key is not specified, the fully qualified name of the class will be used as the key.
     */
    'menus' => [
        'sidebar' => Sidebar::class,
        'top-bar' => new Autowire(TopBar::class),
        'other' => new OtherMenu()
    ]
];

创建菜单

要创建菜单,创建一个类并实现接口 Spiral\KnpMenu\MenuInterface

namespace App\Menu;

use Knp\Menu\FactoryInterface;
use Knp\Menu\ItemInterface;
use Spiral\KnpMenu\MenuInterface;

final class Sidebar implements MenuInterface
{
    public function __construct(
        private readonly FactoryInterface $factory
    ) {
    }

    public function create(array $options = []): ItemInterface
    {
        $menu = $this->factory->createItem('root');

        $menu->addChild('Home', ['route' => 'homepage']);
        $menu->addChild('Blog', ['uri' => '/posts']);
        $menu->addChild('Comments', ['uri' => '#comments']);
        $menu->addChild('Full URL', ['uri' => 'https://site.com/']);

        return $menu;
    }
}

然后,您可以将菜单注册到上述配置文件中,或使用 Spiral\KnpMenu\MenuRegistry 注册。

namespace App\Bootloader;

use App\Menu\Sidebar;
use Spiral\Boot\Bootloader\Bootloader;
use Spiral\KnpMenu\Bootloader\KnpMenuBootloader;
use Spiral\KnpMenu\MenuRegistry;

final class MenuBootloader extends Bootloader
{
    protected const DEPENDENCIES = [
        KnpMenuBootloader::class
    ];

    public function boot(MenuRegistry $registry, Sidebar $menu): void
    {
        $registry->add('sidebar', $menu);
    }
}

使用

如果您正在使用Twig,您可以使用 knp_menu_render 扩展来渲染菜单。

{{ knp_menu_render('sidebar') }}

使用自定义模板或其他选项。

{{ knp_menu_render('sidebar', {'template': 'custom.twig'}) }}

警告 要使用其他模板引擎,需要自行实现菜单显示机制。

测试

composer test
composer psalm
composer cs

变更日志

有关最近更改的更多信息,请参阅 CHANGELOG

许可

MIT许可(MIT)。有关更多信息,请参阅 许可文件