php-lion/navigation 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

php-lion/navigation

Composer 安装命令:

composer require php-lion/navigation

包简介

PhpLion Symfony Navigation

README 文档

README

A Symfony bundle for managing navigation structures using PHP attributes. This bundle provides a declarative way to define navigation menus directly in your controllers.

Description

The PhpLion Navigation Bundle enables developers to define navigation menus using modern PHP 8 attributes, keeping navigation logic close to controller routes. It supports hierarchical menus, navigation groups, automatic active state detection, and flexible rendering.

Features

  • PHP 8 Attributes - Declarative navigation definition using attributes
  • Hierarchical Navigation - Multi-level menu structures with parent-child relationships
  • Navigation Groups - Organize menus into groups (frontend, backend, admin, etc.)
  • Auto-Detection - Automatically mark current/active navigation items
  • Priority System - Control menu item order with configurable priorities
  • Flexible Rendering - Full control over navigation rendering in Twig
  • Type-Safe - Leverages PHP's type system for better IDE support

Requirements

  • PHP >= 8.3
  • Symfony >= 6.4 or 7.0+
  • Composer

Installation

Use Composer to install the package:

composer require php-lion/navigation

Usage

// src/Controller/IndexController.php
namespace App\Controller;

use PhpLion\NavigationBundle\Attribute\Navigation;
use PhpLion\NavigationBundle\Attribute\NavigationGroup;
use Symfony\Bridge\Twig\Attribute\Template;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Attribute\Route;

#[NavigationGroup(name: '_default')]
final class IndexController extends AbstractController
{
    #[Route('/index', name: 'app_index')]
    #[Template('index.html.twig')]
    #[Navigation(path: 'Home')]
    public function index(): array
    {
        return [
            'title' => 'Home',
        ];
    }
{% block navigation %}
    {% for item in  navigation.current %}
        {% if item.children is empty %}
            <a class="nav-link" href="{{ path(item.name) }}">{{ item.label }}</a></li>              
        {% else %}
            {# Do stuff with nested nodes. This nodes will have the same structure as the parent #}
        {% endif %}
    {% endfor %}
{% endblock %}

Keep in mind: The navigation var will be injected to the twig template by ViewEvent. This means your controller hast to returns an array as response. A Symfony\Component\HttpFoundation\Response does not trigger the ViewEvent.

[
    'tree' => [
        '_default' => [
        
        ]    
        'my_navigation_group' => [
            0 => [
                'label' => 'Home',
                'name' => 'app_index',
                'tags' => [],
                'children' => [], /** nested navigation items/nodes */
                'selected' => true, /** is set true if named controller action was called */
                'priority' => 1000, /** order of the items (higher priority items appear first) */
                'link' => true,
                'hide' => [],
                'style' => []
            ]
        ]    
    ],
    'groups' => [
        0 => '_default',
        1 => 'my_navigation_group',
    ],
    'currentGroup' => 'my_navigation_group', /** the navigation group of the currently called controller */
    'currentName' => '' /** name of the current called controller action */
    'current' => [] /** content of tree item named in currentGroup */
]

NavigationGroups

You can use NavigationGroups to create different navigation collections for example for backend and frontend use. If NavigationGroup is not set the NavigationGroup ist set to __default_.

Navigation

Navigation implements the tree in the navigation. You have different options you can use. The only necessary one is the path attribute.

Path

You can separate the level of the tree by a '|' delimiter. It is possible to use the attribute multiple times to place the item on different points in you navigation.

Root path in navigation:

    #[Navigation(path: 'Home')]
    #[Navigation(path: 'Contact')]
 ├── Home  
 └── Contact

Nested nodes in navigation:

    #[Navigation(path: 'Home|Products')]
    #[Navigation(path: 'Home|Portfolio')]
    #[Navigation(path: 'Contact')]
 ├── Home  
 │   ├── Products                      
 │   └── Portfolio  
 └── Contact

Link

You can force the navigation item has a link or not.

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

License

MIT License - see LICENSE file for details

Support

For issues and questions:

Credits

Developed by Denis Juroszek (PhpLion)

php-lion/navigation 适用场景与选型建议

php-lion/navigation 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 12 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 03 月 10 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「symfony」 「menu」 「bundle」 「attributes」 「navigation」 「php-lion」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

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

围绕 php-lion/navigation 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 12
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 13
  • 依赖项目数: 3
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-03-10