Moving Laravel's public folder to another location

When deploying a Laravel app, it’s likely that the public folder will need to be served from another location, since you do not want the entire application to be public as this presents a security risk. Usually this is done with the help of a symbolic link -

ln -s ~/projects/my-cool-app/public/ /path/to/public_html/my-cool-app 

However, if this is not possible or practical for whatever reason, it is possible to move or rename the public folder. To do so you have to subclass the Illuminate\Foundation\Application class and override the publicPath method. Create a class file and place it in your app folder. I created an app/Extensions folder for this, but you can use whatever you want. In this case I need the public folder to be placed outside the default location and have use a different name

<?php
namespace App\Extensions;

use Illuminate\Foundation\Application as LaravelApplication;

/**
 * Custom extension to override Laravel's default public folder location
 */
class Application extends LaravelApplication
{
    public function publicPath()
    {
        return $this->basePath().DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'public_html';
    }
}

Then to make sure your newly extended Application class is used during bootstrapping, modify bootstrap/app.php -

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

Run composer dumpautoload to make sure your new class is autoloaded correctly.

Next, if you are using Elixir for your asset pipeline, remember to configure that to use your new location too. Add this line to your gulpfile.js right below where elixir is imported.

var elixir = require('laravel-elixir');

elixir.config.publicPath = '../public_html/';

And you’re done!