adamb / menu-builder
构建HTML导航
1.0.20
2022-01-24 13:27 UTC
Requires (Dev)
This package is auto-updated.
Last update: 2024-09-24 19:47:29 UTC
README
从PHP数组创建HTML菜单和面包屑菜单项
安装
安装可通过Composer/Packagist进行。
composer require adamb/menu-builder
许可证
此软件根据MIT许可证分发。请阅读LICENSE以获取有关软件可用性和分发的信息。
基本用法
添加菜单项
创建一个新的Navigation
菜单构建实例
<?php $navigation = new Menu\Navigation();
添加一些菜单项
// Add links individually $navigation->addLink('Home', '/', array('link_order' => -1000)); $navigation->addLink('About Me', '/about-me', array('link_order' => 2)); // Add an array of links $navArray = [ [ 'title' => 'My Link', 'uri' => '/my-link-page', 'link_order' => 3 ], [ 'title' => 'Has Children', 'uri' => '/child/', 'link_order' => 4 'children' => [ [ 'title' => 'Second Child', 'uri' => '/child/second', 'link_order' => 2 ], [ 'title' => 'First Child', 'uri' => '/child/first', 'link_order' => 1 ], [ 'title' => 'Last Child', 'uri' => '/child/last', 'link_order' => 3 ], ] ] ]; $navigation->addLinks($navArray);
addLink方法允许以下操作:
$navigation->addLink($title, $uri [, $options = []]);
$title
是一个包含要显示在链接上的文本的字符串$uri
是一个包含链接URI的字符串$options
是一个数组,可以包含以下数组元素 ['label', 'uri', 'fragment', 'title', 'target', 'rel', 'class', 'id', 'link_order', 'active', 'li_class', 'li_id', 'ul_class', 'ul_id', 'children']
设置当前URI
设置菜单的活动链接
$navigation->setCurrentURI('/about-me'); // Makes the about me page the current select item in the menu
渲染菜单
将菜单输出到屏幕
echo($navigation->render());
渲染面包屑菜单
将面包屑菜单输出到屏幕
echo($navigation->renderBreadcrumb());