定制 symkit/menu-bundle 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

symkit/menu-bundle

Composer 安装命令:

composer require symkit/menu-bundle

包简介

Menu Bundle for Symkit

README 文档

README

CI Latest Version PHPStan Level 9

A flexible, SOLID menu system for Symfony. Manage menus from the database or via PHP builders, with support for complex structures (Mega Menus, advanced dropdowns).

Features

  • Database or code: Menus from Doctrine entities (priority) or from tagged builders.
  • Configurable: Enable/disable admin, Twig, AssetMapper, Doctrine independently.
  • Overridable entities: Configure custom Menu / MenuItem FQCNs.
  • Routes in config: Admin routes loaded from a routing file with configurable prefix.
  • Public API: Type-hint on Symkit\MenuBundle\Contract\* interfaces for BC-safe usage.

Installation

composer require symkit/menu-bundle

Register the bundle in config/bundles.php (auto with Flex):

return [
    Symkit\MenuBundle\SymkitMenuBundle::class => ['all' => true],
];

Configuration

All features are enabled by default. Example with explicit options:

# config/packages/symkit_menu.yaml
symkit_menu:
    admin:
        enabled: true
        route_prefix: admin   # prefix for admin routes (default: admin)
    twig:
        enabled: true
    assets:
        enabled: true        # AssetMapper prepend for Stimulus controllers
    doctrine:
        enabled: true
        entities:
            menu: Symkit\MenuBundle\Entity\Menu
            menu_item: Symkit\MenuBundle\Entity\MenuItem

Routes

Include the bundle admin routes in your app (e.g. config/routes.yaml):

symkit_menu:
    resource: '@SymkitMenuBundle/Resources/config/routing.yaml'
    prefix: '%symkit_menu.admin.route_prefix%'

This registers routes such as admin_menu_list, admin_menu_create, admin_menu_edit, admin_menu_item_*, admin_menu_items_json. Change route_prefix in config to alter the URL prefix (e.g. /back-office/menus).

Overriding entities

To use your own Menu or MenuItem classes (e.g. extended with extra fields):

  1. Extend the bundle entities: class App\Entity\Menu extends Symkit\MenuBundle\Entity\Menu.
  2. Configure the FQCNs:
symkit_menu:
    doctrine:
        entities:
            menu: App\Entity\Menu
            menu_item: App\Entity\MenuItem
  1. Map your entities in Doctrine (XML/attributes) as usual.

Usage

Admin UI

With admin.enabled: true and symkit/crud-bundle and symkit/metadata-bundle installed, you get CRUD for menus and items (hierarchy, types, icons). Admin controllers use #[Seo] and #[Breadcrumb] from the metadata bundle when available.

PHP builders

Implement the Contract interface and tag the service:

namespace App\Navigation;

use Symkit\MenuBundle\Contract\MenuNavigationBuilderInterface;
use Symkit\MenuBundle\Contract\MenuItemInterface;
use Symkit\MenuBundle\Model\MenuLink;

class PrimaryNavigationBuilder implements MenuNavigationBuilderInterface
{
    public function build(): array
    {
        return [
            new MenuLink('home', 'Home', '/'),
            // ...
        ];
    }
}
services:
    App\Navigation\PrimaryNavigationBuilder:
        tags:
            - { name: 'symkit_menu.menu_builder', alias: 'primary' }

Active menu

Use the #[ActiveMenu] attribute on controllers:

use Symkit\MenuBundle\Attribute\ActiveMenu;

#[ActiveMenu('primary', 'home')]
public function index(): Response { ... }

Or set it manually:

$menuManager->setActiveId('primary', 'item_id');

Twig

When twig.enabled: true, use the get_menu function:

{% for item in get_menu('primary') %}
    {{ include('@SymkitMenu/components/menu/_' ~ (item.type == 'mega' ? 'mega_menu_full' : (item.type == 'advanced_dropdown' ? 'dropdown_advanced' : (item.type == 'dropdown' ? 'dropdown_simple' : 'link'))) ~ '.html.twig', {item: item}) }}
{% endfor %}

Public API (Contract)

For stable, BC-safe integration, type-hint on interfaces under Symkit\MenuBundle\Contract\:

  • MenuNavigationBuilderInterface: implement build(): array (of MenuItemInterface).
  • MenuItemMapperInterface: implement supports(MenuItem $entity): bool and map(MenuItem $entity, MenuModelFactory $factory): MenuItemInterface.
  • MenuItemInterface: id, label, active state, type.

Models under Symkit\MenuBundle\Model\ (e.g. MenuLink, MenuDropdown) implement these contracts.

Customization

  1. Create a model implementing Symkit\MenuBundle\Contract\MenuItemInterface.
  2. Create a mapper implementing Symkit\MenuBundle\Contract\MenuItemMapperInterface.
  3. Tag the mapper with symkit_menu.menu_item_mapper.

The bundle will use your mapper when it meets the corresponding entity type.

Stimulus / AssetMapper

With assets.enabled: true, the bundle prepends its Stimulus controllers to AssetMapper. Register them in your app (e.g. importmap.php and stimulus_bootstrap.js) as needed for dropdown and mobile menu behaviour.

Contributing

make install
make cs-fix
make phpstan
make test
make quality         # cs-check + phpstan + deptrac + lint + test + infection
make ci              # security-check + quality

License

MIT.

symkit/menu-bundle 适用场景与选型建议

symkit/menu-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 289 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 02 月 22 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 symkit/menu-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 symkit/menu-bundle 我们能提供哪些服务?
定制开发 / 二次开发

基于 symkit/menu-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 289
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 23
  • 依赖项目数: 5
  • 推荐数: 1

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-02-22