DEV Community

Cover image for Laravel 8 Paths Helpers - Explained
Priyank Patel
Priyank Patel

Posted on

Laravel 8 Paths Helpers - Explained

Laravel 8 has a lot of helper functions which provide a convenient way of working with strings, arrays and paths. It is almost impractical to know them all. Many developers know the most common ones, but there are some useful helpers if you dig a little deep inside documentations.

Why to use Path Helpers in Laravel ?

In this article, I will be explaining you about each one of them, Why we use them? In what situaton's do we need to use this laravel helper functions? all with practical code examples of using PATHS in Laravel 8.

There are in total 8 in-built Path Helper functions provided by Laravel 8 Framework. These are basically php methods which are very helpful while providing file downloads to your user or you want to show full url of the resource on the server to the user or admins there are many use cases that's why "Helpers" it is named...

1 - app_path() - The app_path function returns the fully qualified path to your application's app directory.

2 - base_path() - The base_path function returns the fully qualified path to your application's root directory.

3 - config_path() - The config_path function returns the fully qualified path to your application's config directory.

4 - database_path() - The database_path function returns the fully qualified path to your application's database directory.

5 - mix() - The mix function returns the path to a versioned Mix file:

6 - public_path() - The public_path function returns the fully qualified path to your application's public directory.

7 - resource_path() - The resource_path function returns the fully qualified path to your application's resources directory.

8 - storage_path() - The storage_path function returns the fully qualified path to your application's storage directory.

Final thoughts - This is a very basic need of every Laravel application, to find the exact path of the files or resources on the server. Here Laravel provides an easy way to get those info and show it in frontend.

PRO TIP -
You can use these path helpers in route file to get the path.

//How to get base path(Project Root) in Laravel ?
base_path();

**For usage in route file**
Route::get('/', function () {
    return base_path();
});

//How to get storage folder path in Laravel ?
storage_path(); 

//How to get app folder path in Laravel ?
app_path(); 

//How to get config folder path in Laravel ?
config_path(); 

//How to get datbase folder path in Laravel ?
database_path(); 

//How to get Mix Versioned File path in Laravel ?
mix(); 

//How to get public folder path in Laravel ?
public_path(); 

//How to get resource folder path in Laravel ?
resource_path()

//How to get storage folder path in Laravel ?
storage_path()
Enter fullscreen mode Exit fullscreen mode

You can bookmark this page for any future references on Path Helper Functions as i will be updating this article in future with more new releases from Laravel Framework.

Connect with me on Dev.to and Twitter

Top comments (0)