Home >Backend Development >PHP Tutorial >How to Configure Laravel's Public Directory on Shared Hosting with cPanel?
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.
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.
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!