How to Retrieve Directory Paths in Laravel

Laravel Laravel

Introduction

When developing a Laravel project, there are many cases where you need to retrieve the paths of various directories. For example, loading configuration files, saving logs, and executing custom scripts. This article explains how to obtain the paths of different Laravel directories.

Target Audience

  • Those who want to understand Laravel’s project structure
  • Those who need to retrieve directory paths
  • Those who want to utilize Laravel helper functions

Laravel Project Structure

A Laravel project consists of the following directory structure:

my-laravel-app/
├── app/         # Core application logic
├── bootstrap/   # Framework initialization
├── config/      # Configuration files
├── database/    # Migrations and seed data
├── lang/        # Localization files
├── public/      # Public directory
├── resources/   # Views, language files, assets
├── routes/      # Route definitions
├── storage/     # Cache, logs, session data
├── tests/       # Test code
├── vendor/      # Composer dependencies

How to Retrieve Each Directory Path

Laravel provides the following helper functions to retrieve the paths of various directories.

app_path()

To get the path of the app/ directory, use app_path():

$path = app_path();
// Output: /var/www/html/my-laravel-app/app

base_path()

To get the root directory of the project, use base_path():

$path = base_path();
// Output: /var/www/html/my-laravel-app

config_path()

To get the path of the config/ directory, use config_path():

$path = config_path();
// Output: /var/www/html/my-laravel-app/config

database_path()

To get the path of the database/ directory, use database_path():

$path = database_path();
// Output: /var/www/html/my-laravel-app/database

lang_path()

To get the path of the lang/ directory, use lang_path():

$path = lang_path();
// Output: /var/www/html/my-laravel-app/lang

public_path()

To get the path of the public/ directory, use public_path():

$path = public_path();
// Output: /var/www/html/my-laravel-app/public

resource_path()

To get the path of the resources/ directory, use resource_path():

$path = resource_path();
// Output: /var/www/html/my-laravel-app/resources

storage_path()

To get the path of the storage/ directory, use storage_path():

$path = storage_path();
// Output: /var/www/html/my-laravel-app/storage

Summary of Directory Path Retrieval Methods

Helper FunctionDirectory Retrieved
app_path()app/
base_path()Project root
config_path()config/
database_path()database/
lang_path()lang/
public_path()public/
resource_path()resources/
storage_path()storage/

Reference Links from Official Documentation

For more details on Laravel directories, refer to the official documentation:

Laravel10

Laravel11

Conclusion

In this article, we explained how to retrieve the paths of various directories in Laravel. Laravel provides convenient helper functions that make it easy to get directory paths. Utilize the official documentation and apply this knowledge to your project development.

著者

30 years of experience as a web engineer, currently working as a freelance backend engineer.

PHP: ~30 years (Laravel 7 years / FuelPHP 5 years / CakePHP / custom frameworks)
JavaScript: ~20 years (React & Vue, 4 years each)
Cloud & Infrastructure: AWS (EC2, CloudFront, RDS, API Gateway, etc.) / GCP (BigQuery)

I have been writing PHP since version 4, back when the framework ecosystem was fragmented
and every team had their own approach. I've lived through the evolution firsthand —
from raw PHP and homegrown frameworks to the modern Laravel era —
which means I don't just know how to use a tool, but why it exists and what problem it replaced.

I work across system design, implementation, and operations, primarily on backend systems
for both product companies and contract-based projects.

On this blog, I write about the things I actually got stuck on, looked up, or figured out
in real-world projects. If something here unblocks even one person's day, that's enough for me.

千原 耕司をフォローする

役にたったと思ったら応援をお願いします m(._.)m

Laravel
スポンサーリンク
シェアする
千原 耕司をフォローする
Copied title and URL