Skip to content
/ h2l Public

A micro framework for websites and apis for small projects. This is a sequel of h.l, made for PHP 7+

License

Notifications You must be signed in to change notification settings

alkemann/h2l

Repository files navigation

H2L - microframework

Get started quickly with static pages and small apis

Packagist Tag PHP codecov StyleCI Scrutinizer Code Quality

Documentation

Hosted on Github pages: https://alkemann.github.io/h2l/

Requirements

  • PHP 8.1

Install

  • Install via composer: composer require alkemann/h2l
  • While you can use the library as only a lib, it comes with an index.php file. The library comes with 3 skeletons to get you started, the "min" contains basically just the index.php, the minimum expected folder structure and a hello world base route. You can also use the "base" that contains a an app, some base css, config files. Thirdly there is an "example" version that contains some more illustative example pages and dynamic routes. install by simply copying the skeleton folder contents of choice down to the root of your app (presumably the same folder that contains the "vendor" composer): vendor/bin/skeleton base (base automagic website using routes by files and folders). There more other skeleton alternatives:
    • min (bare bones)
    • heroku_react (set up for plug and play heroku app with H2L as backend and an react-redux frontend)
    • heroku_min (set up for plug and play heroku app with H2L)
    • api (no automagic routing, for pure api apps and specifically routed responses)
    • hyper (start out with Tailwind, alpine.js and HTMX for server side "SPA")

Usage from skeleton

  • Change the homepage by changing the file content/pages/home.html.php
  • Add files and folders to content/pages to add fixed routed content
  • Include a route file in webroot/index.php or add to resources/configs/routes.php if you installed the base skeleton.
  • Add dynamic routes there by supplying a regex match on url and a closure handler:

Some example routes:

use alkemann\h2l\{Request, Router, Response, response\Json};

// Get task by id, i.e. GET http://example.com/api/tasks/12
Router::add(
  '|^api/tasks/(?<id>\d+)$|',
  function(Request $request): Response
  {
    $id = $request->param('id'); // from the regex matched url part
    $data_model = app\Task::get($id);
    return new Json($data_model); // since Task model implements \JsonSerializable
  }
);

// http://example.com/version
Router::add('version', function($r) {
	return new Json(['version' => '1.3']);
});

Raw usage

A minimal webroot\index.php could look something like this

$root_path = realpath(dirname(dirname(__FILE__)));
require_once($root_path . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php');

use alkemann\h2l\{Environment, Dispatch};

Environment::setEnvironment(Environment::PROD);
Environment::set([
    'debug' => false,
    'layout_path'  => $root_path . 'layouts' . DIRECTORY_SEPARATOR,
    'content_path' => $root_path .  'pages' . DIRECTORY_SEPARATOR,
]);

$dispatch = new Dispatch($_REQUEST, $_SERVER, $_GET, $_POST);
$dispatch->setRouteFromRouter();
$response = $dispatch->response();
if ($response) echo $response->render();

Tests

To run tests you must can checkout the repo and require with dev and run ./bin/runtests in the same folder as this README.md.

Or to run tests on the vendor included lib into your application, you must also require phpunit; composer require phpunit/phpunit and then you can run h2l tests with vendor/bin/testh2l

About

A micro framework for websites and apis for small projects. This is a sequel of h.l, made for PHP 7+

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published