How To Access The Current Route In Laravel With Example

admin_img Posted By Bajarangi soft , Posted On 17-09-2020

In Laravel Routing is the ability to map URL requests to specific routes. It is simply a way of creating a request URL of your application. In Laravel, all requests are mapped with the help of routes. Basic routing routes the request to the associated controllers.Today we are going to discuss how can we create route in laravel project

How To Access The Current Route In Laravel With Example

Accessing The Current Route

You may use the currentcurrentRouteName, and currentRouteAction methods on the Route facade to access information about the route handling the incoming request:
 

$route = Route::current();

$name = Route::currentRouteName();

$action = Route::currentRouteAction();


Example(1)
get current route name
return \Request::route()->getName(); 
return \Route::currentRouteName(); 


Example(2)
get current route path

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


Example(3)
get current route action

return \Route::getCurrentRoute()->getActionName(); //response App\Http\Controllers\ProductCRUDController@index


Example(4)
get a list of all routes

$routes = \Route::getRoutes();
foreach ($routes as $route) {
    echo $route->getPath().'<br>';
}


Methods used in Route
 

static void     macro(string $name, object|callable $macro)Register a custom macro.
static void  mixin(object $mixin, bool $replace = true)
Mix another object into the class.
static bool
hasMacro(string $name)

Checks if macro is registered.

static mixed
__callStatic(string $method, array $parameters)

Dynamically handle calls to the class.

mixed            
__call(string $method, array $parameters)

Dynamically handle calls to the class.


Refer to the API documentation for both the underlying class of the Route facade and Route instance to review all accessible methods.                              

                                                   

Related Post