Route name in Edge

It is possible to get the route name in Edge?
if so, how can I do it?

thanks in advance.

What do you mean by that? You want to know current route in template?

I want to know the route name.
Like when you define in route.js

Route.get('control-panel/', 'DashboardController.index').as('dashboard').

and then in template when constructing the menu I could use it like this

      class="{{ (routeName == 'dashboard'?'active':'' }}">

instead of

      class="{{ (url == '/control-panel/' ||  url == '/control-panel')?'active':'' }}">

I would recommend you define a method via runtime value, which would do the checking inside your template.

A possible check you can use is a request.match method.

class SomeMiddleware {

  async handle ({ view }, next) {
      isActivePath: (routeComparison) => {
        return request.match([ routeComparison ])

    await next()

Then in your view:

{{ isActivePath('control-panel/*') }}

hello, thanks for your reply.
unfortunately it returns false. I would still have to check if it match with “/” or without it

Well, you can use any other method of comparison you want in the method. This was just a suggestion / example. You can compare by URL, or try to figure out the route name from request.

OK, but request does not carry the route name.
but thanks anyway.

Right, so then you have no way to find out which route your are in anywhere in the application, thus you cannot get the route name in Edge.

But request.match is supposed to do what you need. If it does not work as documented, then I suggest you file a bug.

I don’t know if the match function should recognize the path with and without the last ‘/’.
If yes there’s a bug, but if no it is working well and we are suppose to compare/match the path with both cases.

I don’t think it matches the path. It matches your route definitions.

So if you define your route as users/:id then you should match against that request.match( ['users/:id'] ).

1 Like

During my tests I was changing the match value to be equal to path that’s why it wasn’t working. Thank you

I’m also interested in this. A possible way is to pass the Route name through the controller function?

I thought Route.url() would have this information but it seems the Route is not available in the Controller*.

*disclaimer: new to adonis

I came from Symfony framework and have access to route name make all sense, specially if you’r checking the route in more than one place and you need to change the URL… If one could use route name he would change the URL without the need to go everywhere and change the URL

Having route information in the request / edge templates is very helpful in building navigation and interfacing with roles and permissions.
Most frameworks I’ve worked with have this information readily available.

Just wanted to share how I solved it. Briefly I set up the custom global routeName for all my views in my custom middleware (e.g. SetViewGlobals.js). Then in templates I can use it to conditionally generate the active class for required menu items.

In details:

  1. Create the middleware as per Adonis docs above and fill it with the following:
// in ./app/Middleware/SetViewGlobals.js
'use strict'

const Env = use('Env'),
  Route = use('Route');

class SetViewGlobals {
  async handle({ request, session, view }, next) {'routeName', () => {
      return Route.match(request.url(), request.method(), Env.get('HOST'));
    await next()
module.exports = SetViewGlobals
  1. Then in your Edge template you can use it like this if you named you route:
<li class="{{ routeName() === 'myroutename' ? 'active' : '' }}">
  //...some HTML here

or like this if you did not name your route (the routeName() value becomes a pure URL path, which is the default behaviour of the framework):

<li class="{{ routeName() === '/my/non/named/route/path' ? 'active' : '' }}">
  //...some HTML here

Hope that clarifies things for newcomers a bit more.

1 Like