Generate route url in controller



I want to return a Location header for my response and I would like to be able to use a helper or something.

I saw in adonis docs that there is route() helper for view, I tried to use this in my controller, but it’s not working.

There is something else I could use or I should just use strings?


Hey @melokki! :wave:

Here you go:


thank you for your replay @romain.lanz but this is not what I am looking for.
I have the following code:

return response.header('Location', '')
                          status: 'warning',
                          message: 'This role already exists.',
                          data: role

and in that location header I want to send the route where the resource can be found.

I tried something like this, but It’s not working

return response.header('Location', route('', role))
                          status: 'warning',
                          message: 'This role already exists.',
                          data: role

and I am not getting any errors, but as soon as I remove the route('', role) code everything is working again.


You go an error because the route helper doesn’t exist.

Could you please add more context to your request? It seems very weird to add the Location header to a response that send JSON.


I was thinking that when I try to store a new resource first I will search for it’s name, if the resource exists, I will return a warning and a Location header so the user knows where to find that resource.


Well, that’s not how it works.
The Location header is used to tell the browser where to redirect the user.

Read more about it here:


I understand.
Thank you very much @romain.lanz