Laravel: How to Get Current Route Name? (V5 ... V7)

Laravel: How to Get Current Route Name? (v5 ... v7)

Try this

Route::getCurrentRoute()->getPath();

or

\Request::route()->getName()

from v5.1

use Illuminate\Support\Facades\Route;
$currentPath= Route::getFacadeRoot()->current()->uri();

Laravel v5.2

Route::currentRouteName(); //use Illuminate\Support\Facades\Route;

Or if you need the action name

Route::getCurrentRoute()->getActionName();

Laravel 5.2 route documentation

Retrieving The Request URI

The path method returns the request's URI. So, if the incoming request is targeted at http://example.com/foo/bar, the path method will return foo/bar:

$uri = $request->path();

The is method allows you to verify that the incoming request URI matches a given pattern. You may use the * character as a wildcard when utilizing this method:

if ($request->is('admin/*')) {
//
}

To get the full URL, not just the path info, you may use the url method on the request instance:

$url = $request->url();

Laravel v5.3 ... v5.8

$route = Route::current();

$name = Route::currentRouteName();

$action = Route::currentRouteAction();

Laravel 5.3 route documentation

Laravel v6.x...7.x

$route = Route::current();

$name = Route::currentRouteName();

$action = Route::currentRouteAction();

** Current as of Nov 11th 2019 - version 6.5 **

Laravel 6.x route documentation

There is an option to use request to get route

$request->route()->getName();

How to get current route in laravel blade?

Laravel Routes has method to get it.

To get current route name:

Illuminate\Support\Facades\Route::currentRouteName();

To check the current route is a matching route:

Illuminate\Support\Facades\Route::is('routename');

Laravel: How to Get Current Route Name? (v5 ... v7)

Try this

Route::getCurrentRoute()->getPath();

or

\Request::route()->getName()

from v5.1

use Illuminate\Support\Facades\Route;
$currentPath= Route::getFacadeRoot()->current()->uri();

Laravel v5.2

Route::currentRouteName(); //use Illuminate\Support\Facades\Route;

Or if you need the action name

Route::getCurrentRoute()->getActionName();

Laravel 5.2 route documentation

Retrieving The Request URI

The path method returns the request's URI. So, if the incoming request is targeted at http://example.com/foo/bar, the path method will return foo/bar:

$uri = $request->path();

The is method allows you to verify that the incoming request URI matches a given pattern. You may use the * character as a wildcard when utilizing this method:

if ($request->is('admin/*')) {
//
}

To get the full URL, not just the path info, you may use the url method on the request instance:

$url = $request->url();

Laravel v5.3 ... v5.8

$route = Route::current();

$name = Route::currentRouteName();

$action = Route::currentRouteAction();

Laravel 5.3 route documentation

Laravel v6.x...7.x

$route = Route::current();

$name = Route::currentRouteName();

$action = Route::currentRouteAction();

** Current as of Nov 11th 2019 - version 6.5 **

Laravel 6.x route documentation

There is an option to use request to get route

$request->route()->getName();

Laravel current route name contains

See str_contains helper function

<li class="d-flex flex-column {{ str_contains(Route::currentRouteName(), 'messages') ? 'active' : '' }}">

Beware that this would return true even if the route is /user/chat/messages/unread which is probably not what you want

You may look at starts_with

<li class="d-flex flex-column {{ starts_with(Route::currentRouteName(), 'messages') ? 'active' : '' }}">

Laravel 6+ users

The helper functions no longer ship with the default installation

You need to install the laravel/helpers package

composer require laravel/helpers

Laravel: How do I get the current route

You may use the current, currentRouteName, and currentRouteAction methods to access information about the route handling the incoming request:

$route = Route::current();
$name = Route::currentRouteName();
$action = Route::currentRouteAction();

You can check this Link too

Hope it helps...

Laravel how to get current Route

You can use Request::is('cat/a').



Related Topics



Leave a reply



Submit