How do I get the current URL path in Laravel8

Published January 17, 2022

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 get url of the project

Laravel files will be generated in a new folder, "url_path."

 

Laravel8 get 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:

 

Laravel8 get url path of the current project

 Step 3: To display our current URL path, we need to do this. Add the following code to your welcome.blade.php file:

 

Laravel get current project url path

 

<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.

 

 

Download Source code

 

Article Contributed By :
https://www.rrtutors.com/site_assets/profile/assets/img/avataaars.svg

500 Views