Home >Backend Development >PHP Tutorial >Day Host Static App on GitHub Pages

Day Host Static App on GitHub Pages

Susan Sarandon
Susan SarandonOriginal
2024-12-31 07:45:13643browse

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

  1. 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.

Day  Host Static App on GitHub Pages

Push to GitHub

Initialize Repo
Day  Host Static App on GitHub Pages

Commit the Code
Day  Host Static App on GitHub Pages

Publish the Repo
Day  Host Static App on GitHub Pages

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.

For more similar articles:

  • Jenkins with PHP – Run Your First Pipeline
  • Integrating TikTok API with Laravel: A Comprehensive Guide

The above is the detailed content of Day Host Static App on GitHub Pages. 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