Getting Current user route and path

AI-generated summary

In a Discourse development discussion, Alavi1412 asks how to programmatically determine the current route a user is viewing — such as “homepage” or “category 52” — to build a dynamic widget that adapts per page, without relying on window.location.href due to URL variability across Discourse sites. eviltrout responds by suggesting using getOwner(this).lookup('controller:application').get('currentPath') to retrieve the current route name (e.g., discovery.category). However, Alavi1412 notes this doesn’t reveal dynamic segment details like the category ID. eviltrout clarifies that to access category data (like ID or name), one should query the specific controller: getOwner(this).lookup('controller:discovery.category').get('model'). duranmla questions the advantage over window.location.pathname, to which eviltrout explains that using the model provides richer data and avoids fragility from URL changes (e.g., subfolder installations). The discussion highlights using Discourse’s Ember-based routing and controller architecture for reliable, dynamic page context detection — essential for building reusable, site-agnostic widgets.

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)

If you import getOwner you can do this:

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

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

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');

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

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.