How to get route name by url address

#1

Hi

Assume, this is my routes.js file

Route.get('/post/new', 'PostController.get_new_post').as('get_new_post')

and user opens this URL in the browser

http://localhost:3333/post/new

It’s clear that above URL matches get_new_post route

How can I get the route name by the requested URL ?

request.match isn’t useful here.

any help. thanks

0 Likes

#2

What is the purpose of trying to get the route name of a request that requests that route via its name?

0 Likes

#3

I didn’t get you. please read more carefully.
How are you sure if a request is requested via it’s route name from client-side !?!!
and if so, how it gonna help me find the route name of the request in server side ?!

0 Likes

#4

Well, as a developer you are in control of the client code, I presume.

You can also see the HTTP method which requested the route in question via

const method = request.method()

I am sorry if I misunderstood your question, though.

0 Likes

#5

Well, as a developer you are in control of the client code, I presume.

it’s not possible to find out if the request is from a link which is made by your code inside a view by it’s route name. if you meant this, sorry but it’s nonsense.

the question is simple, get a request and find the related route-name ( if exists ) of the requested URL.

thank your for your comment. any other help ?

0 Likes

#6

How about using the Route class here.

const Route = use('Route')

console.log(
  Route.match('http://localhost:3333/post/new', 'GET')
)
0 Likes