Introduction
Why would you change the public folder path of you laravel app ?
When you try to deploy or publish your Laravel application to a standard web server, you may want to use your web hosting provider’s already existing root folder (public html,.etc).
In that case, in order to use your default web location, you might need to switch the default public folder to a public_html or another folder.
The default folder structure of a Laravel project looks something like this.
The entry point to our app is public
folder which contains index.php
and other public resources of your app.
We will be changing that to a new folder called new_public
.
Get Started
Modify AppServiceProvider.php
There is a function called register in AppServiceProvider.php that can be used to change the public folder. This section typically contains nothing by default. use the code below to define a specific path for the public folder.
// app/Providers/AppServiceProvider.php
public function register()
{
$this->app->bind('path.public', function() {
return base_path().'/new_public';
});
}
When deploying to cPanel, we should modify the public folder to point to the public_html
folder or the main folder for your subdomain.
To avoid breaking your app in the local environment, add a condition that checks if our app is in production before changing the public folder path.
// app/Providers/AppServiceProvider.php
public function register()
{
if($this->app->isProduction()){
$this->app->bind('path.public', function() {
return realpath(base_path('/../../test.yourdomain.com'));
});
}
}
Adjust the path ( ‘/…/…/test.yourdomain.com’ ) according to your folders structure in your server.
Upload files to the new_public folder
We need to create a storage disk before we can upload the new files to our new_public
folder.
To do so, navigate to the config/filesystems.php
file and create a disk as shown below.
// config/filesystems.php
'disks' => [
'public_uploads' => [
'driver' => 'local',
'root' => env('APP_ENV') !== 'production' ?
public_path() . '/uploads' :
realpath(base_path('/../../test.yourdomain.com/uploads')),
'url' => env('APP_URL').'/uploads',
],
.....
]
To avoid breaking our app in the local environment, we must add another condition and check if our app is in production before changing the upload folder path.
In the preceding example,
- In a local environment, the upload folder is
public/upload
. - In a production environment, the upload folder will be
test.yourdomain.com/uploads
.
Use the disk in this manner.
Storage::disk('public_uploads')->putFileAs($request->file('image'),$fileName);
Now if we upload files to the public_uploads
disks it will go to new_public/uploads folder
.
All of your uploads will be publicly accessible if you use Storage::disk(‘public uploads’) disk. If you don’t want any of your files to be accessible to everyone, upload them to another disk.
Move contents of old public to the new public folder
In the last step make sure to copy or move the contents of default public folder to your new one.