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.

コメント

Copied title and URL