flat-file
, (*1)
Fast static-site generator / flat-file CMS, (*2)
View an example project., (*3)
Features
- Needs zero configuration
- Uses Markdown, HTML, or PHP
- Routes based on file system
- Exports static sites, or runs via PHP powered web servers
- Includes development environment
Reasoning
PHP is easy to install, if not already present on you computer. PHP runs pretty much everywhere. PHP is flexible., (*4)
This project also scratches an itch to see how much PHP can handle in this problem domain., (*5)
Requirements
Getting Started
{
"name": "user/site",
"description": "",
"require": {
"slogsdon/flat-file": "dev-master"
},
"scripts": {
"build": "flat-file build",
"start": "flat-file serve"
},
"config": {
"process-timeout": 0
}
}
The scripts
and config
sections are not required, but they do help simplify the development process. If using the start
script, the process-timeout
configuration option allows Composer to run a script for longer than the default timeout (300 seconds)., (*6)
If not using Composer script configurations, you'll need to reference the flat-file
script as vendor/bin/flat-file
, e.g.:, (*7)
vendor/bin/flat-file build
vendor/bin/flat-file serve
After setting up your composer.json
file, don't forget to pull down your dependencies:, (*8)
composer install
Adding Content
Next, your individual pages need to be included in a pages
directory at the root of your project (i.e. next to your composer.json
file). Pages can be written in Markdown (with the .md
or .markdown
file extension), plain HTML, or PHP (with the .php
file extension). PHP files have the option of outputing the content as normal (echo, print, content outside of <?php ?>
tags, etc.) or returning the content as a string (<?php return 'hello world';
)., (*9)
<?php /* pages/index.php */ ?>
<h1>Hello World</h1>
Add more pages with new files under pages
, e.g.:, (*10)
<!-- pages/about.md -->
# About
Running the Development Server
Ready to test? Spin up the development server:, (*11)
composer start
This will start a PHP development server listening on http://localhost:3000
. Pressing Ctrl-C
will stop the server, freeing the way for building your project to plain HTML files for later deployment., (*12)
Building Static Files
Kick off a build of the site:, (*13)
composer build
Ignoring Composer's vendor
directory, you should see something similar to below in your project root once all is said and done:, (*14)
.
โโโ composer.json
โโโ composer.lock
โโโ dist
โ โโโ about.html
โ โโโ index.html
โโโ pages
โโโ vendor
Using a Custom Entrypoint
By default, we provide an entrypoint for the webserver to use when serving requests. It completes some rewrites for files in public
, requires the Composer autoloader, and kicks off the application. You can elect to include your own by creating a new entrypoint named index.php
and put it in the public
directory at the root of your project. Here's the default one for reference:, (*15)
<?php declare(strict_types=1);
$uri = $_SERVER['REQUEST_URI'];
if ($uri !== '/' && file_exists(getcwd() . '/public' . $uri)) {
return false;
}
require getcwd() . '/vendor/autoload.php';
new FlatFile\Application;
Running as a Flat-File CMS
If you wish to run your project with a PHP-enabled web server as a flat-file CMS instead of a static site, you'll want to:, (*16)
- Configure your web root to be
public
- Use a custom entrypoint or configure your web server to have
vendor/slogsdon/flat-file/src/router.php
serve any non-existent paths.
Your web server will then be able to serve static assets from public
as it normally would and also any pages you create., (*17)
F.A.Q.
- What about LiveReload like functionality?
- That's being investigated, but use
F5
/Cmd-R
for the time being.
License
This project is licensed under the MIT License. See LICENSE for details., (*18)