PHP code example of ericktucto / touch

1. Go to this page and download the library: Download ericktucto/touch library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ericktucto / touch example snippets



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();


// code ...
$app->route()->get("/", fn () => Response::html("My index"));
$app->router()->get("/login", [AuthController::class, "login"]);
// AuthController.php
class AuthController
{
    public function login(Request $request)
    {
        // code..
    }
}


// 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


// 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