Get Project Directory in Laravel 10

Get Project Directory in Laravel 10

Laravel provides a plethora of features and enhancements designed to streamline the development process. One frequent need is to dynamically access the project directory in the code. It can be useful for dynamically constructing file paths, managing configurations, or executing other directory-specific operations. This tutorial demonstrates how to get project directory in Laravel 10.

Obtaining the project directory in Laravel is a simple task, accomplished through the use of the base_path function. Below is an example illustrating the usage of this function within a controller:

app/Http/Controllers/TestController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Response;

class TestController
{
    public function index(): Response
    {
        $dir = base_path();

        return new Response($dir);
    }
}

Leave a Comment

Cancel reply

Your email address will not be published.