How do I get the current URL path in Laravel8
It is pretty easy to get the current URL path in Laravel 8. Just follow these steps. Learn Laravel URL handling with tutorials on rrtutors.com. Start coding today!
It is pretty easy to get the current Url Path in Laravel 8. Just follow these steps
Step 1: Set up a new Laravel project by running the following command on the composer
composer create-project --prefer-dist laravel/laravel url_path |
![]() |
Laravel files will be generated in a new folder, "url_path."
![]() |
Step 2: Our laravel project has been created. Go to http://127.0.0.1:8000 in your browser to see this page:
![]() |
Step 3: To display our current URL path, we need to do this. Add the following code to your welcome.blade.php file:
![]() |
<head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Laravel</title> <!-- Fonts --> <link href="https://fonts.googleapis.com/css2?family=Nunito:wght@400;600;700&display=swap" rel="stylesheet"> <!-- Styles --> <style> body { font-family: 'Nunito', sans-serif; } </style> </head> <body class="antialiased"> <?php // this line gets the Current Filename. $currentPage= $_SERVER['SCRIPT_NAME']; // this line gets the directory name where file have been stored $currentPage = substr($currentPage, 1); // this line displays the current Filename echo $currentPage; ?> </body> </html>
|
Step 4: To see your URL path, go to http://127.0.0.1:8000 in your web browser
That’s it, our URL path is displayed on our screen.