GitHub Pages is designed for hosting static sites, which means it doesn't support PHP applications like Laravel out of the box. However, you can host the static frontend part of your Laravel application by exporting the compiled assets (HTML, CSS, and JavaScript) using Laravel's artisan commands and tools.
Here's how you can adapt a Laravel "Hello World" app for GitHub Pages:
Steps to Host Laravel on GitHub Pages
- Prepare Your Laravel App Create a route in routes/web.php for your Hello World application:
Route::get('/', function () {
return view('welcome'); // Or replace 'welcome' with your view file.
});
Ensure your app runs locally with php artisan serve.
Install laravel-export Package
Use the laravel-export package to export your Laravel views as static HTML files.
Install it via Composer:
composer require spatie/laravel-export
Publish the configuration file:
php artisan vendor:publish --provider="Spatie\Export\ExportServiceProvider"
Export the Static Files
Run the following command to export your Laravel routes to static HTML files:
php artisan export
The static files will be saved in the storage/export directory by default (you can change the output path in the config/export.php file).
Copy the Exported Files
Navigate to the storage/export directory and copy all the files to a new folder in your project, e.g., dist.
Push to GitHub
Enable GitHub Pages
- Go to your repository on GitHub.
- Navigate to Settings > Pages.
- Under the Source section, select main branch and set the folder to /root or /docs if needed.
- Save your settings.
Access Your Site
Your site will be live at https://your-username.github.io/your-repo/.
Notes:
- This approach only works for static content. Dynamic Laravel features (e.g., database access, authentication) will not function on GitHub Pages.
- For hosting the full Laravel application, consider using platforms like Heroku, Vercel, or Laravel Forge.
Top comments (0)