Laravel 5.5 change public to public_html

David Carr

Laravel Framework Tutorials PHP & MySQL

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.

Copyright © 2006 - 2024 DC Blog - All rights reserved.