Skip to content

Microframework PHP, create to web applications with Touch

License

Notifications You must be signed in to change notification settings

ericktucto/touch

Repository files navigation

Touch

Microframework PHP, create to web applications with Touch

Table of contents

Install with Composer

composer require ericktucto/touch

Steps to create basic application

1. Create config.yml

env: local # local or production

2. Create index.php file

<?php
require __DIR__ . '/../vendor/autoload.php';

use Touch\Application;
use Touch\Core\Kernel;
use Touch\Http\Response;

$app = Application::create(new Kernel());

$app->route()->get("/", fn() => Response::html("Hello world!!!"));
$app->run();

Integrated packages

  1. PHP Dig (dependency injection container)
  2. Routing by thephpleague (use psr-7 and psr-15 to middleware)
  3. Eloquent (ORM's Laravel)
  4. Twig (engine template of Symfony)
  5. Clockwork (debugger console)
  6. Valitron (validation data request)

Ecosystem

Routing

Create your routing web applications

<?php
// code ...
$app->route()->get("/", fn () => Response::html("My index"));

To more infor, you'll visit docs of The php league's Routing. But Touch have some interesed features.

Response class

Routing always need a function return a variable that implements Psr\Http\Message\ResponseInterface (see doc). To make returning response, exists Touch\Http\Response

<?php
// code ...
// return html or text plain, $status is optional and 200 is default value
$app->route("/html", fn () => Response::html("<h3>My first app</h3>", 200));

// return json, $status is optional and 200 is default value
$app->route("/api/v1/json", fn () => Response::json(["message" => "It's okay"], 200);
// first argument can be a object

Grouping routes

Routing can group routes (see docs), but you have Touch\Http\Route to group on class.

<?php
// index file
$app->group('/admin', new AdminRoutes);
// AdminRoutes.php
use League\Route\RouteGroup;
use Touch\Http\Response;
use Touch\Http\Route;

class AdminRoutes extends Route
{
    protected function routes(RouteGroup $group)
    {
        $group->get('/users', [UserController::class, "create"]);
        $group->get('/list-users', fn() => Response::html("<!-- list users -->"));
    }
}
// routes created
// GET /admin/users -> create method of UserController class
// GET /admin/list-users -> anonymous closure

Releases

No releases published

Packages

No packages published

Languages