hfw/site

Minimal MVC I/O

dev-master 2023-12-08 07:37 UTC

This package is auto-updated.

Last update: 2024-11-08 09:56:26 UTC


README

Route requests without overhead and respond as you like.

When you just need an small MVC to do a thing.

Documentation: https://hfw.github.io/site

composer require hfw/site

Web I/O

index.php

<?php
include 'vendor/autoload.php';
use Helix\Site;
use Helix\Site\Response\View;
$site = new Site;

// "GET /"
$site->get('/', fn() => 'Hello World');

// "GET /foo/123" (regex)
$site->get('#^/foo/(?<id>[0-9]+)$#', function(array $path, Site $site) {
    return new View($site, 'view/foo.phtml', [
        'id' => (int)$path['id']
    ]);
});

view/foo.phtml

<?php
/**
 * @var Helix\Site\Response\View $this
 * @var int $id 
 */
echo $id;

Under the Hood

  • The Site constructor takes care of general environment setup, including error handling and logging.
  • If no routes matched, a 404 or 405 error is rendered.
  • Each HTTP error code can have its own template. A generic template is used as a fallback.

Views

View instances merely include their templates (.phtml files).

Within the scope of those files, $this refers to the View instance itself. Any data given to the view is converted to variables via extract() in the same scope as the template inclusion.

By convention, templates should be stored in the view directory in the document root.

nginx config

server {
    # ... server config ...
    location / {
        # ... fastcgi config ...
        fastcgi_param SCRIPT_FILENAME $document_root/index.php;
    }
}