Tutorials, PHP & MySQL, Laravel Framework

Laravel 5.5 change public to public_html

When moving Laravel onto a production server I like to place all Laravel files in their own directory above the document root. This keeps your files organised but does present a problem since the document root is often a public_html folder.

One solution that I’ve seen suggested a lof is to bind the path fro public in the appServiceProvider.php register method:

public function register()
{
    $this->app->bind('path.public', function() {
        realpath(base_path().'/../public_html');
    });
}

I’ve had mixed results with this, it means the public_path does look at the correct path in controllers but when you need the paths in configs before the provider has had a chance to kick in the original path comes back in.

I’ve found a much more reliable way is to go into your public_html/index.php and add:

$app->bind('path.public', function() {
    return __DIR__;
});

This updates your public path to the current directory, this even works for subfolders say public_html/beta.

After setting the above the public_path() helper correctly returns the new path.

David Carr

David Carr

For the past 12 years, I’ve been developing applications for the web using mostly PHP. I do this for a living and love what I do as every day there is something new and exciting to learn.

In my spare time, the web development community is a big part of my life. Whether managing online programming groups and blogs or attending a conference, I find keeping involved helps me stay up to date. This is also my chance to give back to the community that helped me get started, a place I am proud to be apart of.

Besides programming I love spending time with friends and family and can often be found together going out catching the latest movie, staying in playing games on the sofa or planning a trip to someplace I’ve never been before.