Getting Current user route and path


(SMHassanAlavi) #1

Is there any way to find the current route of user?

for example if user is watching homepage it return something like homepage.

or if user is watching a category with id of 52 (just an example) it returns category 52 or something similar to this to distinguish where the user is.

(I want this for creating a very dynamic widget and it should provide different menu on each page)
(and I don’t want to use window.location.href because url differ from each site to another and I want to create a widget for all discourse sites)


(Robin Ward) #2

If you import getOwner you can do this:

const path = getOwner(this).lookup('controller:application').get('currentPath');

(SMHassanAlavi) #3

thank you so much but this have some problem.
for example when I open http://localhost:3000/c/foodwaste/information It showed discovery.category and it didn’t give any information about category


(Robin Ward) #4

The current route is discovery.category, and the category is known as a dynamic segment. If you need to know information about the category, you can get it from the controller:

const category = getOwner(this).lookup('controller:discovery.category').get('model');

(Alexis Duran) #5

What is the advantage of this over window.location.pathname?


(Robin Ward) #6

It’s retrieving the actual category model so you can get more than just the name.

Additionally, relying on the path name assumes it’ll never change, like subfolder installs and such.