acid-solutions/menufactory

一个用PHP创建酷炫菜单的工具

v0.0.2 2015-05-28 13:13 UTC

This package is not auto-updated.

Last update: 2024-09-28 18:42:12 UTC


README

轻松创建复杂菜单。

如何安装

编辑 app/config/app.php 添加

// 'providers'
'AcidSolutions\MenuFactory\MenuFactoryServiceProvider',

然后

// 'aliases'
'MenuFactory' => 'AcidSolutions\MenuFactory\MenuFactoryFacade',

如何使用

调用工厂

$menu = MenuFactory::createMenu('MenuName');

定义一个或多个子项

$menu->add('Menu without Child');

获取一个菜单的HTML渲染

$menu->renderHtml();
// OR
MenuFactory::render('MenuName');

模式

模式是对标签的装饰

$menu->add('Menu without Child but with pattern')->pattern('<strong>__LABEL__</strong>');
# OR
$pattern = '<strong>__LABEL__</strong>';
$menu->add('Menu without Child but with pattern1')->pattern($pattern);
$menu->add('Menu without Child but with pattern2')->pattern($pattern);
$menu->add('Menu without Child but with pattern3')->pattern($pattern);

链式多级

通过链式添加创建多级菜单

$menu->add('Menu with chained child')->add('First level')->add('Second level')->add('third level')->add('Fourth level');

多个子项与闭包

通过闭包创建多个子项,你可以访问所有选项

$menu->add('Menu with multiple child defined by closure', function ($menu) {
  $menu->add('first child');
  $menu->add('second child');
  $menu->add('third child');
});

更多菜单?好吧,让我们开始吧!

$menu->add('Menu with multiple child with multiple child defined by closure', function ($menu) {
  $menu->add('first child', function ($menu) {
    $menu->add('first child');
    $menu->add('second child');
    $menu->add('third child');
  });
  $menu->add('second child', function ($menu) {
    $menu->add('first child');
    $menu->add('second child');
    $menu->add('third child');
  });
  $menu->add('third child', function ($menu) {
    $menu->add('first child');
    $menu->add('second child');
    $menu->add('third child');
  });
});

通过闭包允许访问

有时你需要根据用户组隐藏或显示菜单项。使用闭包来定义它!

// Using Sentry::check() for this example (it will check if user is auth)
$userAuth = Sentry::check();
$menu->add('Menu with allow function')->allow(function () use ($userAuth) {
  // You can do everything you want here
  return $userAuth;
});

$menu->add('Menu with allow function (gonna be hide)')->allow(function () use ($userAuth) {
  // You can do everything you want here
  return !$userAuth;
});

// Wait, why closure if i've the value?
$menu->add('Menu with allow function (gonna be hide)')->allow($userAuth);

项目选项

将选项应用于项目(class、点击、亲吻)

$menu->add('Menu with Option')->option('class', 'btn');

哦,该死,我想在 a 上添加 btn 类,在 a 的容器 li 上添加 btn-group,如果这个元素有子项,则添加 dropdown-menu...怎么办!

$menu->add('Menu with Option')
  ->option('class', 'btn')
  ->option('class', 'btn-group', 'li')
  ->option('class', 'dropdown-menu', 'ul');

链接怎么办?

哦,是的!链接...

$menu->add('Menu with uri')->uri('to_this_uri');