定制 unstoppablecarl/navinator 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

unstoppablecarl/navinator

Composer 安装命令:

composer require unstoppablecarl/navinator

包简介

A php package for fexibly managing navigation template data.

README 文档

README

Build Status Coverage Status

Navinator is a php package for fexibly managing navigation data for views.

Navinator is a light weight navigation tree helper providing simple collection and node classes designed to allow you to generate navigation tree data without complication.

Installing

  • Install Composer
  • Add unstoppablecarl/navinator to your project's composer.json file:
{
    "require": {
        "unstoppablecarl/navinator": "1.*"
    }
}
  • Install/update
$ cd my_project
$ php composer.phar install

Look at the example files in examples/ to see all the cool things you can do with Navinator.

Design Goals

  • Add nodes to a collection tree without complication
  • Add nodes in any order, parents do not have to be added before children
  • Nodes do not need to know anything about children or parents
  • Use a simple directory-like path to describe the position of a node in the tree
  • Act as a helper that could be used for any view
  • Generate data ready to be used by a view requiring minimal logic and function calls
  • Let the view handle the html

Node Path and Node Url

Each node has a directory-like path that describes where the node is in the tree. In most cases the url of the nav node will match the path. By default slashes are added to the path and set as the 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';

Usage

  1. Create a collection
  2. Add nodes to the collection in any order
  3. Generate the data to be passed to your view.

The following example generates a simple navigation tree.

  • 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
//                        )
//                )
//        )
//)

Views

The output template data can be rendered with a very simple view function.

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
}

A more extensive example of this simple view is available in examples/simple-view/index.php

License

MIT

统计信息

  • 总下载量: 144
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 2
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2013-07-08

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固