PHP code example of unstoppablecarl / navinator

1. Go to this page and download the library: Download unstoppablecarl/navinator library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

unstoppablecarl / navinator example snippets


    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';

  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){