Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

photocurio's avatar

Serving static HTML with Laravel

I have a project in mind to use Laravel to serve static HTML files. These files (~600,000 files) are buried in thousands of subdirectories. The markup in the HTML is obsolete. Its an archive of a 2010s newspaper site that was exported from an old CMS. It has been decided that importing the data into a database is not feasible. In any case, content management is not needed on these archive files, only serving them as updated web pages.

I remember in this episode (https://laracasts.com/series/laravel-8-from-scratch/episodes/8) Jefferey Way explains how to use Laravel to serve content from HTML files with Laravel.

I'd like to use Laravel to do something similar but more complex: resolve the paths to the HTML file, load the markup into PHP's DOMDocument, strip the header and footer, and append a new header and footer. The new file can be cached. It would load updated CSS and JavaScript assets.

I expect it would be possible to generate 'section' views that show paginated lists of pages within a particular subdirectory. This would need a recursive query to dig into the filesystem tree. Presumably these expensive queries can be cached.

I also expect that it would be possible to recursively index the pages for search.

Has Laravel been used in this way before? Are there example apps or tutorials? Got any advice?

About me: I'm a senior developer with a fair bit of enterprise level PHP and Node experience. I have not built anything with Laravel yet.

0 likes
5 replies
LaryAI's avatar
Level 58

Yes, Laravel can be used to serve static HTML files. To achieve the desired functionality of resolving paths to HTML files, loading the markup into PHP's DOMDocument, stripping the header and footer, and appending a new header and footer, the following steps can be taken:

  1. Create a route that accepts a path parameter, which represents the path to the HTML file.
  2. Use the file_get_contents function to read the contents of the HTML file.
  3. Load the contents into PHP's DOMDocument using the loadHTML method.
  4. Use the getElementsByTagName method to select the header and footer elements and remove them from the DOMDocument.
  5. Append the new header and footer to the DOMDocument using the createElement and appendChild methods.
  6. Use the saveHTML method to get the updated HTML markup.
  7. Cache the updated HTML markup using Laravel's caching system.

To generate 'section' views that show paginated lists of pages within a particular subdirectory, a recursive query can be used to dig into the filesystem tree. The results of the query can be cached to improve performance.

To recursively index the pages for search, Laravel's Scout package can be used. Scout provides a simple solution for adding full-text search to Eloquent models. It supports multiple search engines, including Algolia, Elasticsearch, and Meilisearch.

Here's an example implementation of the above steps:

Route::get('/{path}', function ($path) {
    $filePath = storage_path('app/public/' . $path);

    if (!file_exists($filePath)) {
        abort(404);
    }

    $html = file_get_contents($filePath);
    $dom = new DOMDocument();
    $dom->loadHTML($html);

    // Remove header and footer
    $header = $dom->getElementsByTagName('header')->item(0);
    $footer = $dom->getElementsByTagName('footer')->item(0);
    $header->parentNode->removeChild($header);
    $footer->parentNode->removeChild($footer);

    // Append new header and footer
    $newHeader = $dom->createElement('header', 'New Header');
    $newFooter = $dom->createElement('footer', 'New Footer');
    $dom->documentElement->insertBefore($newHeader, $dom->documentElement->firstChild);
    $dom->documentElement->appendChild($newFooter);

    $updatedHtml = $dom->saveHTML();

    // Cache the updated HTML markup
    Cache::put($path, $updatedHtml, 60);

    return $updatedHtml;
});
2 likes
kokoshneta's avatar

Aside from the more specific requirements of stripping and replacing headers and footers, this doesn’t sound wholly dissimilar to what Statamic (also Laravel-based) does.

I’ve never actually used or even played around with Statamic, but depending on your concrete needs, it’s possible that it might save you some time.

2 likes
Snapey's avatar

i would consider crawling the documents and adding the page metadata into a database that can reflect the page heirachy, the content path and metadata like the title. This would be a one-time script and make it easy to build the new page content by using values from the db and then a simple regex query of the page content.

Please or to participate in this conversation.