kebir/menu-generator
Composer 安装命令:
composer require kebir/menu-generator
包简介
Simple Menu Generator
README 文档
README
Menu Generator
Installations
Install through composer:
{
require: {
"kebir/menu-generator": "dev-master"
}
}
Usage
<?php //Given this list of menus $menus = [ ["id" => 1, "name" => "Menu 1", "url" => "test.com", "parent_id" => 3], ["id" => 2, "name" => "Menu 2", "url" => "test.com", "parent_id" => 0], ["id" => 3, "name" => "Menu 3", "url" => "test.com", "parent_id" => 0], ["id" => 4, "name" => "Menu 4", "url" => "test.com", "parent_id" => 2], ["id" => 5, "name" => "Menu 5", "url" => "test.com", "parent_id" => 3], ["id" => 6, "name" => "Menu 6", "url" => "test.com", "parent_id" => 1], ]; //Let's build a hierarchical menus list $builder = new Kebir\MenuGenerator\Builder(); $menus_generated = $builder->build($menus); //Check the output foreach ($menus as $menu) { echo $menu->getName() . "-" . $menu->getUrl()."\n"; foreach ($menu->getElements() as $submenu) { echo "--- ".$submenu->getName()."\n"; } } /** output Menu 2 - test.com --- Menu 4 Menu 3 - test.com --- Menu 1 --- Menu 5 */
If you want to display the menu, the package provides a HtmlListRenderer
class that will display the menus using html <ul> and <li> tags.
The class requires an instance of Kebir\MenuGenerator\Selector\Selector which
is responsible of detecting if a menu should considered as selected.
<?php $current_url = 'http://test.com/page1'; $simple_selector = new Kebir\MenuGenerator\Selector\SimpleUrlSelector($current_url); $renderer = new Kebir\MenuGenerator\Renderer\HtmlListRenderer($simple_selector); echo "<ul>"; foreach ($menus as $menu) { $renderer->render($menu); } echo "</ul>";
Laravel Users
The package includes a Service Provider and a Facade for the Renderer. Edit the app/config/app.php:
<?php //Add the service provider 'Kebir\MenuGenerator\MenuGeneratorServiceProvider' ... //add the facade alias 'MenuRenderer' => 'Kebir\MenuGenerator\Facades\Renderer'
To use it, simply call the following in your blade template for example:
{{ MenuRender::render($menu) }}
Menu Selection
To mark a menu as selected, you can use the Kebir\MenuGenerator\Selector\SimpleUrlSelector class when creating the menu renderer.
For laravel users, the service provider is configured to include the Kebir\MenuGenerator\Selector\LaravelSelector instead. This will allow you, in the config file provided, to define which entry in the menu should be selected when a page is reached.
This is very useful when the current page is not in the menu but is related to another url which is in the menu. Here is an example of usage for the config/config.php file
<?php return array( //To link /users/1/edit to another url /users in the menu. 'linked_urls' => array( '/users/1/edit' => '/users' ), //To link an action to an url 'linked_actions' => array( 'UsersController@edit' => '/users' ), //To link a route to an url 'linked_routes' => array( 'user_edit_path' => '/users' ) );
kebir/menu-generator 适用场景与选型建议
kebir/menu-generator 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 77 次下载、GitHub Stars 达 2, 最近一次更新时间为 2014 年 07 月 02 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 kebir/menu-generator 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 kebir/menu-generator 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 77
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 11
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2014-07-02