Laravel 5 change public_path()
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
Title: Customizing Laravel's Public Path: A Comprehensive Guide for Developers
Introduction: Understandably, as a Laravel developer, you may occasionally need to modify the location of your application's public folder. This blog post aims to provide you with a clear understanding of how to change the public_path() and various approaches to customize this functionality in your project.
Understanding the Public Path:
In Laravel, the public path is established when you runphp artisan serve. It typically points to thepublicfolder within your application's root directory. This path is crucial for accessing static assets like images and stylesheets from your web browser.Changing the Public Path:
There are multiple ways to alter the public path in Laravel applications. Here, we will discuss two methods: configuring using environment variables and changing directly within .env file.
Method 1: Using Environment Variables
Laravel supports configuration through environment variables. To change your public folder's location, you can follow these simple steps:
- Create a
.envfile in the root directory of your application (if one doesn't exist), and open it using your preferred text editor. - Add the following line under
APP_URL=http://localhost:php
PUBLIC_DIR="path/to/your/custom/public"
- Replace "path/to/your/custom/public" with the desired directory path for your public folder.
- Save and close the file, then navigate to your application root directory in your terminal (e.g., `cd /path/to/application/root/`)
- Run the following command: `export PUBLIC_DIR=/path/to/your/custom/public` (make sure to replace with your own path)
- Restart your Laravel development server using the same command as before, e.g., `php artisan serve`.
Method 2: Changing Directly within .env File
Alternatively, you can modify your application's existing public folder and update the `public_path()` variable by following these steps:
- Navigate to your Laravel application root directory in your terminal.
- Open the `.env` file and find the `PUBLIC_DIR` setting (if it doesn't exist, create a new one).
- Replace the current value with the path of the custom public folder: `PUBLIC_DIR=/path/to/your/custom/public`
- Save the changes to the file.
- Run `composer update`, followed by `php artisan config:clear && php artisan cache:clear`. This ensures your application reads the new values from the config file.
Conclusion:
To conclude, Laravel's public path can be altered in a few ways, depending on your preference and project requirements. Following the methods provided here, you can easily customize `public_path()` in your Laravel applications. For more comprehensive tutorials and helpful resources on customizing your Laravel projects, visit https://laravelcompany.com/blog/.