Change public folder in laravel apps

Rajesh KumarRajesh Kumar
2 min read

You have encountered a situation where you need to change public folder in laravel apps either due to shared hosting providers or different directory structure of application. like in Hostinger, you need to rename your public folder to public_html. at many places public_html is default directory from where your application is loaded.

For Laravel 10:

for laravel 10, before you run any custom commands that is of the applications like bagisto has php artisan bagisto:install command, you first rename your public folder to public_html. then you add following line in app/Providers/AppServiceProvider@register method. here is the sample code.

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot(): void
    {

    }

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register(): void
    {
        app()->usePublicPath(base_path('public_html'));
    }
}

then you run your desired command such as bagisto:install for bagisto.

For laravel version < 10.

for these versions.. you still has to do all this before you run or do anything app specific(like above mentioned regarding bagisto).

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {

    }

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
          $this->app->bind('path.public', function() {
               return base_path().'/public_html';
          });
    }
}

then you proceed with the following...

This is it. Thanks...

0
Subscribe to my newsletter

Read articles from Rajesh Kumar directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Rajesh Kumar
Rajesh Kumar

I am developer from Madhepura, Bihar, Graduated from Assam university