ForgeStaticHtml Module
Overview
ForgeStaticHtml is a powerful module that converts your dynamic PHP application into static HTML files. This is particularly useful for improving performance, security, and enabling easy deployment to static hosting platforms.
Configuration
Configure the static site generation through the forge_static_html.php
configuration file in your application:
return [
'output_dir' => 'public/static', // Directory where static files will be generated
'base_url' => '/', // Base URL for the static site
'clean_build' => true, // Clean the output directory before building
'copy_assets' => true, // Copy asset files to the output directory
'asset_dirs' => [ // Directories containing assets to copy
'public/assets',
'public/images'
],
'include_paths' => [ // URLs to generate static pages for
'/',
],
];
Configuration Options
output_dir
: Specifies where the generated static files will be savedbase_url
: The base URL path for the static siteclean_build
: Whether to clean the output directory before generationcopy_assets
: Whether to copy asset files to the output directoryasset_dirs
: List of directories containing assets to be copiedinclude_paths
: List of URL paths to generate static pages for
Usage
Generate static HTML files using the provided command:
php forge.php static:generate:html
This command will:
- Process all configured routes
- Generate static HTML files
- Copy assets to the output directory
- Maintain directory structure
Examples
Here's an example of a typical setup with routes and controllers:
Controller Example
namespace MyApp\Controllers;
use Forge\Core\Helpers\App;
use Forge\Http\Request;
use Forge\Http\Response;
use Forge\Core\Contracts\Modules\ViewEngineInterface;
class HomeController
{
/**
* @inject
*/
private ViewEngineInterface $view;
public function index(Request $request): Response
{
return $this->view->render(view: 'home.index', layout: 'base');
}
public function install(Request $request): Response
{
return $this->view->render(view: 'home.installation', layout: 'base');
}
}
Route Configuration
use Forge\Core\Helpers\App;
use MyApp\Controllers\HomeController;
$router = App::router();
$router->get('/', [HomeController::class, 'index']);
$router->get('/installation', [HomeController::class, 'install']);
When you run the static generation command, it will:
- Generate
index.html
from the home page route - Generate
installation.html
from the installation route - Copy all assets, including CSS and images
- Maintain the layout structure defined in your views
Note: Make sure your views use relative paths for assets and links to ensure proper functionality in the static version.