Documentation

Ordinary PHP. Shared pieces.

A page can be a complete HTML document. Or it can include a header and footer. It’s your PHP.

Include what you want to reuse.

This example shares its document structure with other pages. Edit the footer once and every page that includes it gets the change.

<?php
$title = 'One header. One footer. Your page.';
require __DIR__ . '/../../partials/example-header.php';
?>

<h1>A few familiar pieces.</h1>
<p>The header and footer are ordinary PHP includes.</p>
<p>Change either shared file, and every page using it changes too.</p>

<?php require __DIR__ . '/../../partials/example-footer.php'; ?>

Open the resulting page · Read the header · Read the footer

__DIR__ makes the include relative to the file containing it. No special layout syntax is involved. Variables set before an include are available inside it.

Add a little behavior.

Read a query parameter, loop over an array, call a service, or load your own code. You can connect to a database using PDO or your preferred PHP library; Cornerstone doesn’t require one or choose one for you. This example reads a name and safely puts it into HTML.

<?php
use function Cornerstone\escape;

$name = is_string($_GET['name'] ?? null) ? trim($_GET['name']) : '';
$title = 'A little PHP goes a long way.';
require __DIR__ . '/../../partials/example-header.php';
?>

<h1>Hello, <?= escape($name !== '' ? $name : 'friend') ?>.</h1>
<p>A query string, a variable, and a little HTML.</p>

<form method="get" action="<?= escape($site->url('/examples/greeting')) ?>">
    <label for="name">Your name</label>
    <input id="name" name="name" value="<?= escape($name) ?>" maxlength="80">
    <button type="submit">Say hello</button>
</form>

<?php require __DIR__ . '/../../partials/example-footer.php'; ?>

Try the greeting. You can change the name using its form.

Escape text when it becomes HTML.

Cornerstone\escape() wraps PHP’s htmlspecialchars(). Use it for text and quoted HTML attributes. It does not make a URL safe to redirect to, or escape JavaScript, CSS, or SQL. Validate input for the place you use it.

Use Composer packages.

The starter works without Composer. If you add packages with composer require, it loads Composer’s autoloader automatically.

Add Cornerstone to an existing project.

Install the package with Composer:

composer require madlab/phpcornerstone

Create your own pages/ directory and public/index.php:

<?php
require dirname(__DIR__) . '/vendor/autoload.php';
Cornerstone\Site::run(dirname(__DIR__));

Your page files belong in your own project, outside vendor/. The package supplies the router without replacing your site. Point your web server at public/ and route page requests through its index file, using the Apache or Nginx configuration.

Keep private code private.

Put helpers and data outside pages/ and public/. Inside pages/, files or folders beginning with _ or . are never pages. Only lowercase .php extensions are recognized. Page symlinks are unsupported.

Forms and responses.

Pages accept GET, HEAD, and POST. Read $_GET or $_POST as usual. HEAD runs the page but sends no body, so don’t make changes on GET or HEAD. Other methods receive a 405 response.

For a form that changes data, add server-side validation and CSRF protection. Cornerstone doesn’t supply authentication, sessions, or a form-processing system. The greeting example is a read-only GET form.

You can use PHP’s header() and http_response_code() before output. Pages are buffered until they finish. Avoid exit, flushing output buffers, or streaming responses if you want Cornerstone’s normal response handling.