pavelkucera / navigation
v1.0.0
2013-01-09 20:44 UTC
Requires
- php: >=5.3.0
- nette/nette: *
Requires (Dev)
- phpunit/phpunit: 3.7.*
This package is not auto-updated.
Last update: 2024-09-14 13:35:47 UTC
README
一个简单的工具,帮助您快速创建结构化导航。
许可证
BSD 3-Clause
依赖关系
- Nette 框架 任何版本
- php >= 5.3.0
安装
通过 composer
$ composer install pavelkucera/navigation
使用
我想现在就有一个导航!
创建结构化导航非常简单。
use PK\Navigation\Node; $navigation = new Node(); $navigation->addChild(new Node('Homepage', 'Homepage:')); $navigation->addChild(new Node('Blog:', 'Blog:')); $portfolio = $navigation->addChild(new Node('Portfolio')); $portfolio->addChild(new Node('Nette', 'Portfolio:nette')); $portfolio->addChild(new Node('JavaScript', 'Portfolio:javascript')); $portfolio->addChild(new Node('Android', 'Portfolio:android'));
子项数量没有限制。
我需要渲染它
由于每个网站都有其渲染导航的独特方式,此工具不附带任何“官方”的Nette\Application\UI\Control
实现。不过,您可以在示例目录中找到一个示例。
工具提供了一个易于阅读的视图对象。
use PK\Navigation\Node; use PK\Navigation\NodeView; $navigation = new Node('navigation'); $navigation->addChild(new Node('Homepage', 'Homepage:')); $navigation->addChild(new Node('Blog:', 'Blog:')); $view = $navigation->render(new NodeView()); $view->label; // 'navigation' $view->link; // NULL $view->active; // FALSE $view->children; // array(2)
我需要设置顺序
将子项优先级作为第二个参数传递(优先级越高越好)。在优先级相同的情况下,时间至关重要(越早越好)。
use PK\Navigation\Node; $navigation = new Node(); $navigation->addChild(new Node('Homepage', 'Homepage:'), 1); $navigation->addChild(new Node('Blog:', 'Blog:'), 5);
我需要标记活动节点
有一个方法可以做到这一点!
use PK\Navigation\Node; $navigation = new Node(); $navigation->addChild(new Node('Homepage', 'Homepage:'), 1); $navigation->addChild(new Node('Blog:', 'Blog:'), 5); $navigation->resolveActive(function($link) { return $presenter->isLinkCurrent($link); });
它总是在整个节点树上调用。
当一个节点活动时,我需要其父节点也处于活动状态
将TRUE
作为第二个参数传递。
$navigation->resolveActive($callback, TRUE);
我需要获取活动节点数组
use PK\Navigation\NodeView; $navigation->renderActiveNodes(new NodeView());
方法将调用此方法的节点渲染到给定的视图对象中,并将所有活动子节点(包括嵌套的子节点)包含到属性$children
中。
并非所有人都可以看到所有导航项
您可以根据用户角色或用户权限限制访问。请注意,您不能同时使用这两种方法。
use PK\Navigation\Node; use PK\Navigation\NodeView; $roleRestricted = new Node('the Jedi Temple', 'Entrance:'); $roleRestriected->restrictAccess('jedi'); // only jedi can access the jedi temple $roleRestriected->render(new NodeView(), function($role) { return $role === 'jedi'; }); $permissionsRestricted = new Node('Abydos', 'Stargate:abydos'); $permissionsRestricted->restrictPermissions('startgate', 'access'); // only people with access to a stargate can travel to Abydos $permissionsRestricted->render(new NodeView(), function($resource, $privilege) { return $resource === 'stargate' && $privilege === 'access'; });
将回调函数传递给渲染方法至关重要,它决定了节点是否被渲染。如果回调返回TRUE
,则节点将被渲染,否则不会。