How to get route name on visit URL in laravel

You can get route name on visit URL in Laravel using request() method. You can use same code snippet in web.php, controller and view file to get the route name from URL In laravel

Answers 2
  • Get route name using request() function on visit URL in laravel

    //Syntax
    request()->route()->getName()
    
    //Demo code with output
    Route::get('/get-route-name', function(){
        echo request()->route()->getName();
    })->name('route.name');
    
    0

    Output:

    route.name

    This code snippet will help you to get the route name on visit a specific url in laravel. You have to just pass the name to route using name method while defining the route in web.php file. 

    Just copy/paste code in route\web.php file and visit /get-route-name url after running php artisan serve command to get the current route name.

  • Get route name using request() method on visit URL in laravel view

    {{  request()->route()->getName() }}
    
    0

    Output:

    home

    Using this code snippet you can get the route name of current URL in laravel blade (view) file. You have to just specify the name for a route using name method on route definition in web.php.

  • Back to code snippet queries related laravel