daniel-samson/teensyphp

The minimalists web framework

Installs: 162

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 0

Forks: 0

pkg:composer/daniel-samson/teensyphp


README

Teensy PHP is a micro web framework for rapidly creating REST APIs and hypermedia.

Project Status

Build codecov CodeFactor

Features

  • Lightweight Framework
  • Simple Router
  • PHP Templating Engine
  • Middleware Support
  • Easy to inject or replace functionality (its just some small functions)
  • env Config Utility
  • Database Connection Manager Utility
  • ArrayAccessImplementation, Crud Traits

Examples

<?php
// routes/web.php
use App\Actions\Home\DisplayHome;

routerGroup("/", function () {
    // Homepage
    route(method(GET), url_path('/'), DisplayHome::class);

    // Example url parameter
    route(method(GET), url_path_params("/hello/:name"), function () {
        render(200, html_out(template('src/templates/hello.php', ['name' => $_GET[':name']])));
    });
});
<?php
// routes/api.php

// /api/{route}
routerGroup("/api", function () {
    // API home
    route(method("GET"), url_path("/"), function () {
        render(200, json_out(["message" => "Hello World!"]));
    });

    // Example JSON body (echo server)
    route(method(POST), url_path("/echo"), function () {
        $body = json_in();
        render(201, json_out($body));
    });
});
<?php
// App/Actions/Home/DisplayHome.php
namespace App\Actions\Home;

class DisplayHome
{
    public function __invoke()
    {
        $accept = request_header('Accept');
        if ($accept === 'application/json') {
            render(200, json_out(['message' => 'Hello World']));
        } else {
            render(200, html_out(template(app_root() . "/templates/pages/home.php", [])));
        }
    }
}

Requirements

  • PHP 8.0+
  • Composer

Documentation

Please see the wiki for more information on how to rapidly create apps with teensyphp.

Creating a new project

Install teensyphp command line tool

composer global require daniel-samson/teensyphp

Create a new project

teensyphp new project-name
cd project-name
composer install

Start buit in web server

composer dev

Update Teensyphp

composer global update