Skip to content
Tutorials Center
My learning

Topics

Lesson 6 of 39 ยท Routing and Templates

Router Basics: Routes, Nesting and Dynamic Segments

Learn how the Ember router maps URLs to routes, how nested routes and the index route work, how dynamic segments and wildcards capture values, and how templates nest.

The router is the heart of an Ember application. It watches the URL, decides which routes are active, and tells each one to load its data and render its template. Because the URL is the starting point of everything, learning the router first makes the rest of the framework fall into place.

Real versus stand-in. The Router.map snippets and route classes are real Ember code, read-only here. The runnable blocks are JavaScript stand-ins: small matchers that imitate how the router turns a URL into a list of active routes and parameters. Ember's real router is more powerful, but this is the idea.

The route map

All URLs are declared in app/router.js:

JavaScript
// app/router.js
import EmberRouter from '@ember/routing/router';
import config from 'blog/config/environment';

export default class Router extends EmberRouter {
  location = config.locationType;
  rootURL = config.rootURL;
}

Router.map(function () {
  this.route('about');
  this.route('contact', { path: '/get-in-touch' });
  this.route('posts', function () {
    this.route('new');
    this.route('post', { path: '/:post_id' });
  });
  this.route('not-found', { path: '/*path' });
});

Reading it top to bottom you get this table:

URL Route name Notes
/ index Implicit; every app has it.
/about about The path defaults to the route name.
/get-in-touch contact path overrides the URL, the name stays.
/posts posts.index Every nested block has an implicit index.
/posts/new posts.new Static child.
/posts/42 posts.post :post_id is a dynamic segment.
/anything/else not-found *path is a wildcard catch-all.

Route names are dotted paths (posts.post) and are what you use everywhere in code (links, redirects). URLs can change without breaking anything, which is a good reason never to hard-code them.

Nested routes and outlets

The posts block is a parent route with children. When the URL is /posts/42, two routes are active: posts and posts.post. Their templates nest inside each other through outlets:

flowchart TD
  A["application template"] -- outlet --> B["posts template"]
  B -- outlet --> C["posts/post template"]

Each route template renders its child inside its own {{outlet}}.

JavaScript
// app/templates/posts.gjs : the shared frame (e.g. a heading and a sidebar)
<template>
  <h2>Posts</h2>
  <section>
    {{outlet}}
  </section>
</template>
JavaScript
// app/templates/posts/post.gjs : only the post itself
<template>
  <article>{{@model.title}}</article>
</template>

The parent's template stays on screen while you move between its children. That is how Ember keeps menus and layouts stable and does the minimum work on each navigation.

The index route

Inside this.route('posts', function () { ... }), an index route is created automatically for the path /posts. Use it for the "list" screen. Because index is the parent's default child, app/templates/posts/index.gjs renders the list while posts.gjs renders the frame.

Dynamic segments

A path piece beginning with a colon, like :post_id, captures a value from the URL. The route's model() hook receives it:

JavaScript
// app/routes/posts/post.js
import Route from '@ember/routing/route';

export default class PostsPostRoute extends Route {
  model(params) {
    return fetch(`/api/posts/${params.post_id}`).then((r) => r.json());
  }
}

The segment name determines the key in params. Names ending in _id are only a convention; :slug gives params.slug.

Wildcards for "not found"

A path like /*path matches anything not matched earlier. It is how you show a custom 404 page inside the app. Declare it last, because the router tries routes in order of specificity and a catch-all should only get leftovers.

How matching works, as code

The stand-in below turns a route map into patterns and finds the active chain for a URL. It supports static segments, :dynamic segments and a *wildcard.

JS A tiny route matcher

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Note the order of declaration: /posts/new is listed before /posts/:post_id. Ember's recognizer prefers static segments over dynamic ones automatically; this simple stand-in relies on order. Try swapping them and see what /posts/new becomes.

The active route chain

For posts.post there are three active routes: application (always), posts and posts.post. Hooks run from the outside in, and each template renders into the previous one's outlet. The next block lists the chain for a route name:

JS Which routes are active?

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Route options you will use

  • { path: '/custom' }: change the URL, keep the name.
  • { resetNamespace: true }: define a nested route that is named as top-level (rare).
  • Nested functions: this.route('posts', function () { ... }) creates a parent. Do not nest more than two or three levels; deep trees are hard to read. Prefer flatter URLs.

The router also exposes location: history (clean URLs, the default in modern apps), hash (#/about) or none (tests). With history, your server must send all paths to index.html.

Common mistakes

  • Forgetting that a nested block needs {{outlet}}. If the child never appears, the parent template is missing its outlet.
  • Using a URL where a route name is expected. @route="posts/new" is wrong; the name is posts.new.
  • Catch-all placed first. It swallows everything. Put wildcards last.
  • Hard-coding URLs in fetch or links. Use names in the app and keep URLs in router.js only.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which file declares the URLs of an Ember app?

    Router.map in app/router.js is the URL map. Route classes in app/routes/ implement the behavior of each route.
  2. 2. What is the route name of the URL /posts/42 given the map above?

    The post route is nested inside posts, so its full name is posts.post. The 42 is just the value of the dynamic segment post_id.
  3. 3. What does a path of '/*path' do?

    It is a wildcard (glob) segment. It is commonly used for a not-found page and should be declared last.
  4. 4. When /posts/42 is active, which templates are on screen?

    Parent routes stay active, so their templates keep wrapping the child.

Summary

  • Router.map in app/router.js is the single URL map; route names are dotted (posts.post) and are what code uses.
  • Nested route blocks create parents; every block has an implicit index child.
  • :segment captures a value for model(params); *path is a catch-all; declare wildcards last.
  • Parent templates stay rendered and show their child in {{outlet}}.

Next: Route Classes and the Model Hook