autumnrustle
autumnrustle

Reputation: 653

How to get uri from routs using route name in laravel

In routes/web.php I have route

Route::get('/answers', [StaticPagesController::class, 'answers'])->name('answers.index');

In my application I using route name to generate url

route('answers.index') // result is http://example.com/answers

But how can I get only uri (/answers), using route name?

To add .active class to nav items (bootstrap 4) I use next construction

{{request()->is('/answers') ? 'active' : ''}}

But in this case, the whole meaning of named routes is lost. If I change the uri, then I have to change it in all places where I use this approach.

How can I get uri using route name?

Upvotes: 1

Views: 752

Answers (1)

lagbox
lagbox

Reputation: 50491

You can check if the current route is named something:

Route::currentRouteNamed('answers.index')

request()->route()->named('answers.index')

These take patterns so you could do other checks if needed:

...->named('answers.*')

Upvotes: 2

Related Questions