Change public path in Laravel 5.*

Most shared host use public_html instead of public,
While sometimes considered bad practice, some client may force you to drop-in everything via FTP

There are several ways to change public folder name found on Google.

But most of them won’t work perfectly.

  1. In AppServiceProvider.php, bind ‘path.public’ to IoC container
    Won’t work in console, vendor:publish still publish in public path
  2. In index.php, bind ‘path.public’ to IoC container
    Works in console, but artisan serve still won’t work

After dig down deep in the source code, I found the cause:

The publicPath() method in Illuminate\Foundation\Application
is hardcoded.

public function publicPath()
{
    return $this->basePath.DIRECTORY_SEPARATOR.'public';
}

But we should never hard the core, so let’s override this method on our own class.

1. Create app/Application.php

<?php 
namespace App;

class Application extends \Illuminate\Foundation\Application
{
    public function publicPath()
    {
        return $this->basePath . DIRECTORY_SEPARATOR . 'public_html';
    }
}

2. Edit bootstrap/app.php, change:

$app = new Illuminate\Foundation\Application(
    realpath(__DIR__.'/../')
);

to

$app = new App\Application(
    realpath(__DIR__.'/../')
);

Reference: http://stackoverflow.com/questions/31758901/laravel-5-change-public-path

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.