Whether displayed as a website menu or navigation in your mobile app, navigation builds a hierarchy in your project.If you model your navigation, content creators can manage navigation menus without developer assistance.
This way, items that you link in the subpages or linked items element also hold the content. While this approach is more straightforward for understanding the concept, it isn’t suitable for omnichannel delivery.That’s why it’s more suitable for smaller projects or projects that use one way of navigation for all channels.
Navigation separate from content
If you separate navigation from content, you’re creating two items for content linked from your navigation. The first is for the content itself; the second is for a navigation item that links the first one.
This way is better for larger projects prepared for omnichannel content.
Build your navigation
1. Create a content type for navigation items
Create a new content type and add elements needed for your channel. In the code examples below, this type is called the Navigation item. It typically contains:
A text element for the title of the navigation item (Title in the code sample)
A subpages or linked items element for child menu items (Subitems)
A linked items element for the content itself
A URL slug element if you’re building website navigation (URL)
2. Define your root
You need to define where your navigation begins. While websites typically have a homepage, other structures may have a root or main screen. You can use the content type created for navigation for the root navigation item or create a different content type for this purpose.Websites usually benefit from having a separate content type for their homepage. That’s because homepages have a unique appearance and contain a different layout and content than other pages.The code example below uses the navigation item for the homepage, created with the name Root navigation item.
3. Create your navigation items
Create content items using the created navigation content type and link them with the subpages or linked item element.
When finished, publish the root together with all its linked items.
4. Retrieve your navigation items
Developers now need to request the items via the Delivery API to generate your menu.Specify the depth parameter to retrieve more than one level of subpages or linked items if you want to show all navigation with one API call.
5. Display navigation
Using the retrieved data, display the navigation as you prefer. It can be your sections in a mobile app or smartwatch, or it can be a menu on a website like this:
The content of the linked items is inside the modular_content property of the response.And that’s it! You now have a dynamic menu that your content creators can manage entirely from Kontent.ai.
Choose URL structure
When building a web app, you need to decide what your URLs are going to look like. Do you need hierarchical URLs or short URLs? Do you need the URLs to be human-readable? Let's look at several options you can choose from.
Microsites and proofs of conceptIf you’re building a microsite or just a quick proof of concept and need navigation fast, you can hardcode the navigation. No part of your navigation is modeled in Kontent.ai; it’s all in the code. See our sample application as an example. We discourage using hardcoded navigation for larger live projects.
// Tip: Find more about Java SDK at https://kontent.ai/learn/javaimport kontent.ai.delivery.*;// Initializes a DeliveryClientDeliveryClientclient = newDeliveryClient("<YOUR_PROJECT_ID>");// Registers the model class for navigation items// Tip: Create strongly typed models according to https://kontent.ai/learn/strongly-typed-modelsclient.registerType(NavigationItem.class);// Gets navigation items and their linked itemsCompletionStage<NavigationItem> root = client.getItem("root_navigation_item",NavigationItem.class,DeliveryParameterBuilder.params() .linkedItemsDepth(5) .build()// Registers the model class for articlesclient.registerType(Article.class);// Gets specific elements of all articlesCompletionStage<List<Article>> articles = client.getItems(Article.class, DeliveryParameterBuilder.params() .projection("title", "url") .build());