darkling/nette-menu
Composer 安装命令:
composer require darkling/nette-menu
包简介
Menu for Nette framework
README 文档
README
Nette menu
Nette component for creating menus, breadcrumbs and sitemaps.
Requirements
- PHP >=7.1
- Nette ^2.4
Installation
Install package with composer:
$ composer require carrooi/nette-menu
Register as nette extension:
extensions: menu: Carrooi\Menu\DI\MenuExtension menu:
Basic usage
You can write menu links as associated multi dimensional arrays. Because of this you are able to create any structure of menus and submenus you may need.
menu: front: items: Home: action: Front:Home: Books: link: '#' items: All: action: Front:Books:all Featured: action: Front:Books:featured admin: items: Users: action: Admin:Users: Books: action: Admin:Books:
<?php namespace App; use Carrooi\Menu\UI\IMenuComponentFactory; use Carrooi\Menu\UI\MenuComponent; use Nette\Application\UI\Presenter; final class BasePresenter extends Presenter { private $menuFactory; public function injectBasePresenter(IMenuComponentFactory $menuFactory) { $this->menuFactory = $menuFactory; } protected function createComponentMenu(): MenuComponent { return $this->menuFactory->create('front'); } }
{control menu} <!-- display menu -->
{control menu:breadcrumbs} <!-- display breadcrumbs -->
{control menu:sitemap} <!-- display sitemap -->
That structure in neon config will generate two menus:
front:
- Home (action:
Front:Home:) - Books (link:
#)- All (action:
Front:Books:all) - Featured (action:
Front:Books:featured)
- All (action:
admin:
- Users (action:
Admin:Users:) - Books (action:
Admin:Books:)
Templates
This package includes 3 default templates (menu, breadcrumbs, sitemap). However only the default sitemap template should be used in real project. The other two templates should only help you in the beginning with building your own templates which will fit your's website look.
Changing templates can be done in your menu configuration:
menu: front: templates: menu: %appDir%/path/to/custom/menu.latte breadcrumbs: %appDir%/path/to/custom/breadcrumbs.latte sitemap: %appDir%/path/to/custom/sitemap.latte
As you can see, each menu can have different templates.
Visibility of items
It may be useful to hide some links in specific situations. For that we have the visibility option on items where
you can tell on which template the link should be visible.
menu: front: items: Home: action: Front:Home: visibility: menu: true breadcrumbs: false sitemap: true
Mark active item via regex
Menu item can be labeled as active by a regular expression (or array of regular expressions) that is compared to the entire Presenter's name and action.
You can set your regular expression via include setting.
menu: front: items: Home: action: Front:Home: include: '^Front\:Home\:[a-zA-Z\:]+$' # mark as active for all actions of "Front:Home:" presenter Books: action: Front:Books: include: # mark as active for actions "Front:Books:default" and "Front:Books:edit" - '^Front\:Books\:default$' - '^Front\:Books\:edit$'
Translations
When displaying title of link in some template, we always work with translated titles.
You have three options for translator:
- Do nothing: Original
ReturnTranslatorclass will be used. This translator just returns the given text. - Set translator manually: Provide your own implementation of
Nette\Localization\ITranslator. - Set translator to
true: Menu extension will try to find your translator in DI container automatically.
services: - App\MyOwnFrontTranslator menu: front: translator: @App\MyOwnFrontTranslator admin: translator: true
Custom data
Every link can contain additional data which can be later used eg. in your custom latte templates.
menu: admin: items: Adminer: link: http://localhost:20000 data: icon: fa fa-database attrs: target: _blank
<a href="{$item->getRealLink()" n:attr="(expand) $link->getData('attrs')"> <i class="{$item->getData('icon')"></i> {$link->getRealTitle()} </a>
Authorization
Sometimes you may want to hide some links based on custom rules, that includes for example authorization from nette.
This menu package uses custom IAuthorizator interface which you can use to write your own authorizators.
<?php namespace App; use Carrooi\Menu\IMenuItem; use Carrooi\Menu\Security\IAuthorizator; final class FrontAuthorizator implements IAuthorizator { public function isMenuItemAllowed(IMenuItem $item): bool { return isItemAllowed($item); } }
services: - App\FrontAuthorizator menu: front: authorizator: @App\FrontAuthorizator
Link generator
When you want to display a link in your template it uses the ILinkGenerator interface to generate it from the data
in your menu config. If you want to change the default logic (which uses link method from nette) you can just implement
your custom link generator.
<?php namespace App; use Carrooi\Menu\IMenuItem; use Carrooi\Menu\LinkGenerator\ILinkGenerator; final class FrontLinkGenerator implements ILinkGenerator { public function link(IMenuItem $item): string { return generateLink($item); } }
services: - App\FrontLinkGenerator menu: front: linkGenerator: @App\FrontLinkGenerator
You can also override link generator later for some subtree of links:
menu: front: items: Home: action: Front:Home: Books: link: '#' linkGenerator: @App\BooksLinkGenerator
Menu loader
If you want to build your menu maybe from database instead of neon config, you can do that by creating IMenuLoader
class.
See the default ArrayMenuLoader how it works.
Changelog
-
2.1.0
-
2.0.0
- Refactoring - complete rewrite
-
1.1.1
- Move under Carrooi organization
-
1.1.0
- Support for ACL permissions (thanks whipsterCZ)
- Support for generating sitemap (thanks whipsterCZ)
-
1.0.4
- Added
hasIconandhasCountermethods - Support for "absolute" targets #6
- Added
-
1.0.3
- Added shortcut methods for working with item's data #5
-
1.0.2
- Support for "dynamic" modules in includes option #4
-
1.0.1
-
1.0.0
- First version
darkling/nette-menu 适用场景与选型建议
darkling/nette-menu 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 7.17k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2019 年 01 月 22 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「menu」 「breadcrumbs」 「nette」 「control」 「component」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 darkling/nette-menu 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 darkling/nette-menu 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 darkling/nette-menu 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Breadcrumbs integration for Laravel 6
A Symfony extension to get active class base on current bundle/controller/action
Navigation bundle for Symfony applications
Nette Framework adapter for I18n package
Build forms from schema
Asynchronous MQTT client built on React
统计信息
- 总下载量: 7.17k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 7
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-01-22