unstoppablecarl/navinator

一个用于灵活管理导航模板数据的PHP包。

1.0.6 2013-11-19 18:24 UTC

This package is not auto-updated.

Last update: 2024-09-23 13:31:32 UTC


README

Build Status Coverage Status

Navinator 是一个用于灵活管理视图导航数据的PHP包。

Navinator 是一个轻量级的导航树助手,提供简单的集合和节点类,旨在让您无需复杂操作即可生成导航树数据。

安装

  • 安装 Composer
  • unstoppablecarl/navinator 添加到项目的 composer.json 文件中
{
    "require": {
        "unstoppablecarl/navinator": "1.*"
    }
}
  • 安装/更新
$ cd my_project
$ php composer.phar install

查看 examples/ 中的 示例文件,了解Navinator可以完成的所有酷炫功能。

设计目标

  • 无需复杂操作即可向集合树中添加节点
  • 可以以任何顺序添加节点,无需先添加父节点
  • 节点无需了解关于子节点或父节点的内容
  • 使用类似目录的简单路径来描述节点在树中的位置
  • 作为可被任何视图使用的助手
  • 生成供视图使用的数据,逻辑和函数调用最少
  • 让视图处理HTML

节点路径和节点URL

每个节点都有一个类似目录的 路径,描述节点在树中的位置。在大多数情况下,导航节点的URL将与路径匹配。默认情况下,路径中会添加斜杠,并设置为URL。

    use \Navinator\Node;

    $path = 'articles';
    $node = new Node($path);
    $url = $node->url; // /articles/

    $path = 'articles/tags';
    $node = new Node($path);
    $url = $node->url; // /articles/tags/

    $path = 'articles/tags/news';
    $node = new Node($path);
    // set the url manually
    $node->url = 'http://www.theonion.com';

用法

  1. 创建一个集合
  2. 以任何顺序向集合中添加节点
  3. 生成传递给视图的数据。

以下示例生成一个简单的导航树。

  • my-favorite-sites
    • google search
      • maps
      • gmail
    • github
      • gist
  use \Navinator\Collection;
  use \Navinator\Node;
  $collection = new Collection();

  // create a node passing the node's path
  // note: a node with path 'my-favorite-sites' has not been added to the collection yet and does not need to be
  $node = new Node('my-favorite-sites/google');
  $node->url = 'http://google.com';
  $node->display_name = 'Google Search';
  $collection->addNode($node);

  $node = new Node('my-favorite-sites');
  $node->display_name = 'My Favorite Sites';
  // if $node->url is not set, the node path is used:
  // same as: $node->url = '/my-favorite-sites/';
  $collection->addNode($node);

  // create a node object from an array
  $node = new Node(array(
      'path'         => 'my-favorite-sites/github',
      'url'          => 'http://github.com',
      'display_name' => 'Github'
  ));
  $collection->addNode($node);

  $node = new Node(array(
      'path' => 'my-favorite-sites/github/gist',
      'url'  => 'http://gist.github.com'
  ));
  // if $node->display_name (array key or property) is not set, the last segment of the the node path is used: same as $node->display_name = 'gist';
  $collection->addNode($node);

  $node = new Node(array(
      'path' => 'my-favorite-sites/google/maps',
      'url'  => 'https://www.google.com/maps/'
  ));
  // the display order of a node in relation to it's siblings can be set as the optional second param of $collection->addNode()
  $collection->addNode($node, 2);

  $node = new Node(array(
      'path' => 'my-favorite-sites/google/gmail',
      'url'  => 'https://mail.google.com'
  ));
  $collection->addNode($node, 1);

  $templateData = $collection->prepareForNavTemplate();

  print_r($templateData);

// output
// the following node array keys are replaced by [...] to make this example easier to read:
//  - template_data
//  - is_first_child
//  - is_last_child
//  - is_current_root
//  - is_current
//  - is_current_ancestor

//  Array
//(
//    [0] => Array
//        (
//            [url] => /my-favorite-sites/
//            [path] => my-favorite-sites
//            [display_name] => my-favorite-sites
//            [depth] => 1
//            [...],
//            [children] => Array
//                (
//                    [0] => Array
//                        (
//                            [url] => http://google.com
//                            [path] => my-favorite-sites/google
//                            [display_name] => Google Search
//                            [depth] => 2
//                            [...],
//                            [children] => Array
//                                (
//                                    [0] => Array
//                                        (
//                                            [url] => https://mail.google.com
//                                            [path] => my-favorite-sites/google/gmail
//                                            [display_name] => gmail
//                                            [depth] => 3
//                                            [...],
//                                            [children] => Array()
//                                            [display_order] => 1
//                                        )
//                                    [1] => Array
//                                        (
//                                            [url] => https://www.google.com/maps/
//                                            [path] => my-favorite-sites/google/maps
//                                            [display_name] => maps
//                                            [depth] => 3
//                                            [...],
//                                            [children] => Array()
//                                            [display_order] => 2
//                                        )
//                                )
//                            [display_order] => 1
//                        )
//                    [1] => Array
//                        (
//                            [url] => http://github.com
//                            [path] => my-favorite-sites/github
//                            [display_name] => Github
//                            [depth] => 2
//                            [...]
//                            [children] => Array
//                                (
//                                    [0] => Array
//                                        (
//                                            [url] => http://gist.github.com
//                                            [path] => my-favorite-sites/github/gist
//                                            [display_name] => gist
//                                            [depth] => 3
//                                            [...],
//                                            [children] => Array()
//                                            [display_order] => 1
//                                        )
//                                )
//                            [display_order] => 2
//                        )
//                )
//        )
//)

视图

输出模板数据可以使用非常简单的视图函数进行渲染。

function renderSimpleNav($nodes, $depth = 1){
    ?>
    <ul class="depth-<?= $depth ?>">
        <?php foreach($nodes as $node):
            $isFirstChild =
        ?>
            <li>
                <a href="<?= $node['url'] ?>"><?= $node['display_name'] ?></a>
                <?php
                if($node['children']){
                    renderSimpleNav($node['children'], $depth + 1);
                }
                ?>
            </li>
        <?php endforeach; ?>
    </ul>
    <?php
}

更详细的简单视图示例可在 examples/simple-view/index.php 中找到

许可证

MIT