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.

Laravel Modules Your Logo Your Logo Your Logo

Become a sponsor

Help support the blog so that I can continue creating new content!

Sponsor

My Latest Book

Modular Laravel Book - Laravel: The Modular way

Learn how to build modular applications with Laravel Find out more

Subscribe to my newsletter

Subscribe and get my books and product announcements.

Fathom Analytics $10 discount on your first invoice using this link

© 2006 - 2024 DC Blog. All code MIT license. All rights reserved.