Black Friday sale is now on! 50% off Laravel: The Modular Way. Learn more

Laravel 5.5 change public to public_html

David Carr

Laravel Framework PHP & MySQL Tutorials

    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 Book by David Carr

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

    Subscribe to my newsletter

    Subscribe and get my books and product announcements.

    © 2009 - 2022 DC Blog. All code MIT license. All rights reserved.