Home >Backend Development >PHP Tutorial >How to Configure Laravel's Public Directory on Shared Hosting with cPanel?

How to Configure Laravel's Public Directory on Shared Hosting with cPanel?

Susan Sarandon
Susan SarandonOriginal
2024-12-05 01:19:09876browse

How to Configure Laravel's Public Directory on Shared Hosting with cPanel?

How to Configure the Laravel Public Directory Location

When using Laravel on shared hosting with cPanel, it can be challenging to work correctly due to the default root directory being public_html instead of public. This article provides a comprehensive solution to overcome this issue.

Changing the Laravel Public Directory Location

To make Laravel seamlessly use public_html as the public directory, a simple modification can be made to the index.php file. Add the following code snippet before the $app initialization:

// Set the public path to this directory
$app->bind('path.public', function() {
    return __DIR__;
});

This code binding instructs Laravel to recognize the directory where index.php is located as the public directory, ensuring proper operation.

Alternative Solution

As suggested by Burak Erdem, an alternative and preferred approach is to configure the public path in the AppServiceProvider register() method located in AppProvidersAppServiceProvider. The following code can be added:

/**
 * Register any application services.
 *
 * @return void
 */
public function register()
{
    // ...

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

In this solution, the path to public_html is explicitly specified, providing a more robust and maintainable configuration.

The above is the detailed content of How to Configure Laravel's Public Directory on Shared Hosting with cPanel?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn