alkemann / h2l
A micro framework for websites and apis. This is a sequel of h.l, made for PHP 8
Installs: 1 410
Dependents: 0
Suggesters: 0
Security: 0
Stars: 6
Watchers: 2
Forks: 0
Open Issues: 1
Requires
- php: ^8.1
Requires (Dev)
- phpstan/phpstan: ^0.12.4
- phpunit/phpunit: ^9
Suggests
- ext-curl: For remote API usage
- ext-pdo: For the MySql, Postgres and Sqlite Datasources.
- alkemann/debug: Convenient var_dump replaces for debug and development, require-dev only
- dev-master
- v0.64.0
- v0.62.0
- v0.61.0
- v0.60.0
- v0.59.0
- v0.58.0
- v0.57.0
- v0.56.1
- v0.56.0
- v0.55.0
- v0.54.0
- v0.53.0
- v0.52.3
- v0.52.2
- v0.52.1
- v0.52.0
- v0.51.0
- v0.50.0
- v0.49.0
- v0.48.1
- v0.48.0
- v0.47.3
- v0.47.2
- v0.47.1
- v0.47.0
- v0.46.2
- v0.46.1
- v0.46.0
- v0.45.1
- v0.45.0
- v0.44.0
- v0.43.1
- v0.43.0
- v0.42.0
- v0.41.0
- v0.40.0
- v0.39.1
- v0.39.0
- v0.38.0
- v0.37.0
- v0.36.0
- v0.35.0
- v0.34.0
- v0.33.1
- v0.33.0
- v0.32.2
- v0.31.2
- v0.31.1
- v0.31.0
- v0.30.0
- v0.29.0
- v0.28.3
- v0.28.2
- v0.28.1
- v0.28.0
- v0.27.2
- v0.27.1
- v0.27.0
- v0.26.1
- v0.26.0
- v0.25.1
- v0.25.0
- v0.24.0
- v0.23.0
- v0.22.1
- v0.22.0
- v0.21.0
- v0.20.2
- v0.20.1
- v0.20.0
- v0.19.1
- v0.19.0
- v0.18.1
- v0.18.0
- v0.17.1
- v0.17.0
- v0.16.3
- v0.16.2
- v0.16.1
- v0.16.0
- v0.15.0
- v0.13.1
- v0.13.0
- v0.12.0
- v0.11.1
- v0.11.0
- v0.10.0
- v0.9.1
- v0.9.0
- 0.8.5
- 0.8.4
- 0.8.3
- 0.8.2
- 0.8.1
- 0.8.0
- 0.7.0
- 0.6.0
- 0.5.2
- 0.5.1
- 0.5.0
- 0.4.0
- 0.3.0
- 0.2.2
- 0.2.0
- 0.1.0
- dev-dispatch_refector
This package is auto-updated.
Last update: 2024-11-24 17:47:35 UTC
README
Get started quickly with static pages and small apis
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 toresources/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