PHP code example of malinichevvv / lift-php

1. Go to this page and download the library: Download malinichevvv/lift-php 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/ */

    

malinichevvv / lift-php example snippets



Lift\App;
use Lift\Http\Request;
use Lift\Http\Response;

$app = new App();

$app->get('/', fn() => ['message' => 'Hello, World!']);

$app->get('/users/{id:\d+}', function (Request $req) use ($repo) {
    $user = $repo->find((int) $req->param('id'));
    return $user ?? throw new \Lift\Exception\NotFoundException();
});

$app->post('/users', function (Request $req) use ($repo) {
    $data = $req->validate([
        'name'  => '

$app->get('/path',    $handler);
$app->post('/path',   $handler);
$app->put('/path',    $handler);
$app->patch('/path',  $handler);
$app->delete('/path', $handler);
$app->any('/path',    $handler);
$app->map(['GET', 'POST'], '/path', $handler);

// Regex-constrained parameter
$app->get('/posts/{id:\d+}', fn(Request $req) => ['id' => (int) $req->param('id')]);

// Named route + URL generation
$app->get('/users/{id}', $h)->name('users.show');
$app->url('users.show', ['id' => 42]); // → /users/42

// Group with shared prefix + middleware
$app->group('/api/v1', function ($g) {
    $g->get('/users',      [UserController::class, 'index']);
    $g->post('/users',     [UserController::class, 'store']);
    $g->get('/users/{id}', [UserController::class, 'show']);
})->middleware(AuthMiddleware::class);

// Bind interface → concrete class (autowired)
$app->bind(UserRepositoryInterface::class, MySQLUserRepository::class);

// Singleton factory
$app->singleton(Connection::class, fn() => Connection::fromEnv());

// Pre-built instance
$app->instance(Config::class, $config);

// Inject anywhere via type-hint — Lift resolves it automatically
$app->get('/users', function (Request $req, UserRepository $repo) {
    return $repo->all();
});

use Lift\Debug\ErrorRenderer;

// Content-negotiating handler — JSON for API clients, HTML for browsers
$app->onError(ErrorRenderer::auto());

// Or custom
$app->onException(\Lift\Exception\NotFoundException::class, fn() =>
    Response::json(['error' => 'Not found'], 404)
);

use Lift\Testing\TestCase;

final class UserApiTest extends TestCase
{
    public function testCreateUser(): void
    {
        $res = $this->post('/users', ['name' => 'Alice', 'email' => '[email protected]']);

        $this->assertStatus(201, $res);
        $this->assertJsonContains(['name' => 'Alice'], $res);
    }
}
bash
composer