Route name in Edge


#1

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

thanks in advance.


#2

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


#3

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

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

instead of

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

#4

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) {
    view.share({
      isActivePath: (routeComparison) => {
        return request.match([ routeComparison ])
      }
    })

    await next()
  }
}

Then in your view:

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

#5

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


#6

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.


#7

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


#8

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.


#9

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.


#10

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'] ).


#11

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


#12

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


#13

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


#14

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.