Laravel Get Route Name from Given Url

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 get route from given URL

https://laracasts.com/discuss/channels/laravel/how-to-get-a-route-from-uri

try {
$url = 'url';
$route = app('router')->getRoutes()->match(app('request')->create($url))
} catch (NotFoundHttpException $e) {
$route = null;
}

In case of non-get routes:

$method = 'POST';
$url = 'url';
$route = app('router')->getRoutes()->match(app('request')->create($url , $method))

How can I get a route url pattern by route name in laravel?

You can access the Route's uri as follows:

$url_pattern = app('router')->getRoutes()->getByName('items.item')->uri;
var_dump($url_pattern);
// will return:
// "items/{id}/{slug}"

You can always create a faux route. When you are invoking route(), one has to assume you know the params it expects.

If you would like to get it as a string (to be used in JS for example), just pass the names of the params as the params. For example:

$url_pattern = route('items.item', ['id' => '{id}', 'slug' => '{slug}']);
// will generate:
// items/{id}/{slug}

Laravel: Get URL from routes BY NAME

$url = route('routeName');

if there is a param

$url = route('routeName', ['id' => 1]);

https://laravel.com/docs/5.1/helpers#method-route

How to get Route name in Laravel 8

In your View :

{{Route::current()->getName()}}

If you want to show the current route name in the browser Tab foreach pages
add this to your layout view

<title>{{Route::current()->getName()}}</title>


Related Topics



Leave a reply



Submit