定制 feskol/php-navigation 二次开发

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

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

feskol/php-navigation

Composer 安装命令:

composer require feskol/php-navigation

包简介

A simple and effective Navigation to manage active states in navigation structures.

README 文档

README

GitHub Release GitHub License Packagist Downloads codecov

Overview

A simple and effective Navigation to manage active states in navigation structures.
For Symfony integration, check out the NavigationBundle.

Features

  • Automatically tracks a link's active status and makes it easy to check if a parent navigation item has active child links.
  • Easy to set up and integrate into existing projects.
  • Flexible and extensible for complex navigation structures.

Installation

Install via Composer:

composer require feskol/php-navigation

Usage

Building Your Navigation Structure

use Feskol\Navigation\Link;
use Feskol\Navigation\Navigation;

// create links
$navLink = new Link();
$navLink->setTitle('Company')
    ->setHref('/company');
    
$subNavLink = new Link();
$subNavLink->setTitle('About us')
    ->setHref('/company/about-us')
    ->setIsActive(true);
    
// add the $subNavLink as $navLinks Child
$navLink->addChild($subNavLink);


// To have all links in one place you can use the provided Navigation class:
$navigation = new Navigation();
$navigation->setTitle('MyNavigation');

// add the created $navLink to the Navigation
$navigation->addLink($navLink);

Iterating through Navigation links:

foreach ($navigation->getLinks() as $link){
    echo $link->getTitle(); // "Company"
    echo $link->hasActiveChildren(); // "true" - because the child is active
    
    foreach($link->getChildren() as $subLink){
        echo $link->getTitle(); // "About us"
        echo $link->isActive(); // "true"
    }
}

Common link data

The Link class provides setter and getter methods for the common attributes of an <a>-tag.
The most commonly used ones are href="" and target="":

use Feskol\Navigation\Link;

$link = new Link();

// href
$link->setHref('/company/about-us');
$link->getHref(); // "/company/about-us"

// target
$link->setTarget(Link::TARGET_BLANK);
$link->getTarget(); // "_blank"

To view all available methods for the common link data, check out the AbsctractHyperLink class.

Additional Data

There are often situations where you need additional data for your navigation, such as icons or images.
The best approach is to create your own class (e.g. MyCustomLink) that extends the Link class:

use Feskol\Navigation\Link;

class MyCustomLink extends Link
{
    private ?string $icon = null;
    
    public function getIcon(): ?string {
        return $this->icon;
    }
    
    public function setIcon(?string $icon): static {
        $this->icon = $icon;
        return $this;
    }
}

Then use your MyCustomLink class instead of the Link class:

$link = new MyCustomLink();
$link->setTitle('Company')
    ->setHref('/company')
    ->setIcon('bi bi-user'); // For example, using Bootstrap-Icon classes

Handling translations in your frontend

Symfony

In Symfony, you can use the TranslatableMessage class to hold translation infos which you can use in your frontend.
In Twig, apply the |trans filter to translate the TranslatableMessage.

Both the Navigation and Link classes accept any object implementing the \Stringable interface in their setTitle() methods. This allows you to pass objects like TranslatableMessage or custom classes with a __toString() method.

Example in Symfony

use Feskol\Navigation\Link;
use Feskol\Navigation\Navigation;
use Symfony\Component\Translation\TranslatableMessage;

$navigation = new Navigation();
$navigation->setTitle(new TranslatableMessage('MyNavigation', [], 'navigation'));

$navLink = new Link();
$navLink->setTitle(new TranslatableMessage(
    'Nav-Item for %customerName%',
    ['%customerName%' => $customer->getName()], // Dynamic translation parameters 
    'navigation' // Translation domain
));

$navigation->addLink($navLink);

And then in your Twig template:

{# @var \Feskol\Navigation\Navigation navigation #}  

Translated Navigation Title: {{ navigation.title|trans }}  

{% for link in navigation.links %}  
    Translated Link Title: {{ link.title|trans }}  
{% endfor %}  

Contribution Guidelines

We welcome contributions to this project! To ensure clarity and fairness for all contributors, we require that all contributors sign our Contributor License Agreement (CLA).

By signing the CLA, you confirm that:

  1. You grant us the perpetual, worldwide, non-exclusive, royalty-free, irrevocable right to use, modify, sublicense, and distribute your contribution as part of this project or any other project.
  2. You retain ownership of your contribution, but grant us the rights necessary to use it without restriction.
  3. Your contribution does not violate the rights of any third party.

How to Sign the CLA

Before submitting a pull request, please sign the CLA using the following link:
Sign the CLA

Contributions cannot be merged unless the CLA is signed.

Thank you for your contributions and for helping us build something great!

Testing

We're using phpunit.

Run tests:

php vendor/bin/phpunit

❤️ Support This Project

If you find this project helpful and would like to support my work:

  • 🌟 Star the repository to show your appreciation.
  • 💸 Donate via:
    • Buy Me a Coffe: Buy Me A Coffee
    • PayPal: PayPal

Thank you for your support!

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

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

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-01-12