Skip to content

Navigation Menus

Introduction

A great time-saving feature of HydePHP is the automatic navigation menu and documentation sidebar generation. Hyde is designed to automatically configure these menus for you based on the content you have in your project.

There are two types of navigation menus in Hyde:

  • Primary Navigation Menu: This is the main navigation menu that appears on most pages of your site.
  • Documentation Sidebar: This is a sidebar that appears on documentation pages and contains links to other documentation pages.

HydePHP automatically generates all of these menus for you based on the content of your project, and does its best to automatically configure them in the way that you most likely want them to be.

Of course, this won't always be perfect, so thankfully Hyde makes it a breeze to customize these menus to your liking. Keep on reading to learn how! To learn even more about the sidebars, visit the Documentation Pages documentation.

Quick primer on the internals

It may be beneficial to understand the internal workings of the navigation menus to take full advantage of the options.

In short, both navigation menu types extend the same class (meaning they share the same base code), this means that the way they are configured is very similar, making the documentation here applicable to both types of menus.

See the Digging Deeper section of this page if you want the full scoop on the internals!

Primer on priorities

All navigation menu items have an internal priority value that determines their order in the navigation. Lower values mean that the item will be higher up in the menu. The default for pages is 999 which puts them last. However, some pages are autoconfigured to have a lower priority, for example, the index page defaults to a priority of 0,

What to customize?

Here is a quick overview of what you might want to customize in your navigation menus:

  • Navigation menu item labels - the text that appears in the menu links
  • Navigation menu item priority - control the order in which the links appear
  • Navigation menu item visibility - control if pages may show up in the menus
  • Navigation menu item grouping - group pages together in dropdowns

How and where to customize?

Hyde provides a few different ways to customize the navigation menus, depending on what you prefer.

Specifying the data in the front matter will override any dynamically inferred or config-defined priority. While this is useful for one-offs, it can make it harder to reorder items later on as you can't see the whole picture at once. It's up to you which method you prefer to use.

To customize how a page is represented in navigation, you can either set the navigation front matter data in the page's markdown file, or configure it in the config file. Main navigation items are in the hyde config file, while documentation sidebar items are in the docs config file. General options for the entire navigation menus are also available in the hyde and docs config files.

Now that you know the basics, let's dive into the details of how to customize the navigation menus!

Front Matter Configuration

The front matter options allow you to customize the navigation menus on a per-page basis. Here is a quick reference of the available options. The full documentation of each option is below. You don't need to specify all the keys, only the ones you want to customize.

1navigation:
2 label: string # The text to display
3 priority: int # Order is also supported
4 hidden: bool # Visible is also supported (but obviously invert the value)
5 group: string # Category is also supported

label

The label option allows you to customize the text that appears in the navigation menu for the page.

1navigation:
2 label: "My Custom Label"

priority

The priority option allows you to control the order in which the page appears in the navigation menu. You can also use order instead of priority.

1navigation:
2 priority: 10

hidden

The hidden option allows you to control if the page appears in the navigation menu. You can also use visible instead of hidden, but obviously invert the value.

1navigation:
2 hidden: true

group

The group option has a slightly different meaning depending on the type of navigation menu. For the primary navigation menu, it allows you to group pages together in dropdowns. For the sidebar, it allows you to group pages together in the sidebar under a common heading. You can also use category instead of group.

1navigation:
2 group: "My Group"

Config File Configuration

Next up, let's look at how to customize the navigation menus using the config files.

  • To customize the navigation menu, use the setting navigation.order in the hyde.php config.

  • When customizing the navigation menu, you should use the route key of the page.

  • To customize the sidebar, use the setting sidebar_order in the docs.php config.

  • When customizing the sidebar, can use the route key, or just the page identifier of the page.

Changing the priorities

The navigation.order and sidebar_order settings allow you to customize the order of the pages in the navigation menus.

Basic syntax for changing the priorities

The cleanest way is to use the list-style syntax where each item will get the priority calculated according to its position in the list, plus an offset of 500. The offset is added to make it easier to place pages earlier in the list using front matter or with explicit priority settings.

Filepath: config/hyde.php
1'navigation' => [
2 'order' => [
3 'home', // Gets priority 500
4 'about', // Gets priority 501
5 'contact', // Gets priority 502
6 ]
7]
Filepath: config/docs.php
1'sidebar_order' => [
2 'readme', // Gets priority 500
3 'installation', // Gets priority 501
4 'getting-started', // Gets priority 502
5]

Explicit syntax for changing the priorities

You can also specify explicit priorities by adding a value to the array key:

Filepath: config/hyde.php
1'navigation' => [
2 'order' => [
3 'home' => 10, // Gets priority 10
4 'about' => 15, // Gets priority 15
5 'contact' => 20, // Gets priority 20
6 ]
7]
Filepath: config/docs.php
1'sidebar_order' => [
2 'readme' => 10, // Gets priority 10
3 'installation' => 15, // Gets priority 15
4 'getting-started' => 20, // Gets priority 20
5]

You can of course also combine these methods if you want:

Filepath: Applicable to both
1[
2 'readme' => 10, // Gets priority 10
3 'installation', // Gets priority 500
4 'getting-started', // Gets priority 501
5]

Changing the menu item labels

Hyde makes a few attempts to find a suitable label for the navigation menu items to automatically create helpful titles.

From the Hyde config you can override the label of navigation links using the by mapping the route key to the desired title. This is not yet supported for the sidebar, but will be in the future.

Filepath: config/hyde.php
1'navigation' => [
2 'labels' => [
3 'index' => 'Start',
4 'docs/index' => 'Documentation',
5 ]
6]

Excluding Items (Blacklist)

Sometimes, especially if you have a lot of pages, you may want to prevent links from showing up in the main navigation menu. To remove items from being automatically added, simply add the page's route key to the blacklist.

Filepath: config/hyde.php
1'navigation' => [
2 'exclude' => [
3 '404'
4 ]
5]

Adding Custom Navigation Menu Links

You can easily add custom navigation menu links similar to how we add Authors. Simply add a NavItem model to the navigation.custom array.

When linking to an external site, you should use the NavItem::forLink() method facade. The first two arguments are the destination and label, both required. The third argument is the priority, which is optional, and defaults to 500.

Filepath: config/hyde.php
1use Hyde\Framework\Features\Navigation\NavItem;
2 
3'navigation' => [
4 'custom' => [
5 NavItem::forLink('https://github.com/hydephp/hyde', 'GitHub', 200),
6 ]
7]

Simplified, this will then be rendered as follows:

1<a href="https://github.com/hydephp/hyde">GitHub</a>

Configure subdirectory handling

Within the Hyde config you can configure how subdirectories should be displayed in the menu.

Filepath: config/hyde.php
1'navigation' => [
2 'subdirectories' => 'dropdown'
3]

Dropdown means that pages in subdirectories will be displayed in a dropdown menu, while flat means that pages in subdirectories will be displayed as individual items in the menu. Hidden means that pages in subdirectories will not be displayed in the menu at all.

Automatic menu groups

HydePHP has a neat feature to automatically place pages in dropdowns based on subdirectories.

Automatic navigation menu dropdowns

For pages that can be in the main site menu, this feature needs to be enabled in the hyde.php config file.

Filepath: config/hyde.php
1'navigation' => [
2 'subdirectories' => 'dropdown',
3],

Now if you create a page called _pages/about/contact.md it will automatically be placed in a dropdown called "About".

Automatic documentation sidebar grouping

This feature works similarly to the automatic navigation menu dropdowns, but instead places the sidebar items in named groups. This feature is enabled by default, so you only need to place your pages in subdirectories to have them grouped.

For example: _docs/getting-started/installation.md will be placed in a group called "Getting Started".

Tip: When using subdirectory-based dropdowns, you can set their priority using the directory name as the array key.

Digging Deeper into the internals

While not required to know, you may find it interesting to learn more about how the navigation is handled internally. The best way to learn about this is to look at the source code, so here is a high-level overview with details on where to look in the source code.

The main navigation menu is the NavigationMenu class, and the documentation sidebar is the DocumentationSidebar class. Both extend the same BaseNavigationMenu class:

1use Hyde\Framework\Features\Navigation\NavigationMenu;
2use Hyde\Framework\Features\Navigation\DocumentationSidebar;
3use Hyde\Framework\Features\Navigation\BaseNavigationMenu;

Within the BaseNavigationMenu class, you will find the main logic for how the menus are generated, while the child implementations contain the extra logic tailored for their specific use cases.

All the navigation menus store the menu items in their $items array containing instances of the NavItem class.

The NavItem class is a simple class that contains the label and URL of the menu item and is used to represent each item in the menu. Dropdowns are represented by DropdownNavItem instances, which extend the NavItem class and contain an array of additional NavItem instances.

1use Hyde\Framework\Features\Navigation\NavItem;
2use Hyde\Framework\Features\Navigation\DropdownNavItem;