1. Go to this page and download the library: Download migears/web 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/ */
migears / web example snippets
// resources/Users/___user_id___/Index.php
namespace App\Resources\Users\UserId;
use MiGears\Web\AbstractResource;
use MiGears\Web\Request;
use MiGears\Web\Response;
class Index extends AbstractResource
{
public function GET(Request $request): Response
{
$userId = (int) $this->param('user_id');
return Response::json(['id' => $userId, 'name' => 'User ' . $userId]);
}
public function PUT(Request $request): Response
{
$userId = (int) $this->param('user_id');
$body = $request->body;
return Response::json(['id' => $userId, 'updated' => $body], 200);
}
protected function before(Request $request): ?Response
{
// Returning a Response short-circuits dispatch
if ($request->header('X-Auth') === '') {
return Response::json(['error' => 'unauthorized'], 401);
}
return null;
}
protected function after(Request $request, Response $response): Response
{
// Modify the response and return it
return $response->withHeader('X-Powered-By', 'migears');
}
}
use MiGears\Web\MiRest;
use MiGears\Web\Request;
$rest = new MiRest(
baseDir: __DIR__ . '/resources',
namespace: 'App\\Resources',
);
// Register only what needs configuration
$rest->set(PDO::class, fn() => new PDO('mysql:host=localhost;dbname=app', 'user', 'pass'));
$response = $rest->handle(Request::fromGlobals());
$response->send();
$rest->set(string $id, callable $factory): self // Register a factory (lazy)
$rest->has(string $id): bool // Registered (factory pending or resolved)?
$rest->get(string $id): mixed // Resolve the instance (cached for reuse)
$rest->set(PDO::class, fn() => new PDO('mysql:host=localhost;dbname=app', 'user', 'pass'));
$rest->has(PDO::class); // true — factory registered (not yet resolved)
$rest->get(PDO::class); // 1st call runs the factory, caches the result
$rest->get(PDO::class); // 2nd call returns the SAME cached instance
$rest->get('missing'); // throws NotFoundException — an unregistered id is an assembly mistake
class Users extends AbstractResource
{
public function GET(Request $request): Response
{
$pdo = $this->resolve(PDO::class); // shared PDO from the container
$logger = $this->resolve(LoggerInterface::class); // and the shared logger
$dao = new UserDao($pdo, $logger); // plain `new` — no container needed
return Response::json($dao->getAll());
}
}
$rest = new MiRest(baseDir: __DIR__ . '/resources', namespace: 'App\\Resources');
$rest->set(PDO::class, fn() => new PDO('mysql:host=localhost;dbname=app', 'user', 'pass'));
$rest->set(\Psr\Log\LoggerInterface::class, fn() => new Monolog\Logger('app'));
$rest->set(RedisCache::class, fn() => new RedisCache(new Redis()));
public function handle(Request $request): Response
$rest->before(callable $handler): self // Global before hook
$rest->after(callable $handler): self // Global after hook
$rest->notFound(callable $handler): self // Custom 404 handler
$rest->error(callable $handler): self // Custom exception handler
$rest->set(string $id, callable $factory): self // Register an entry (lazy)
$rest->has(string $id): bool // Is it registered?
$rest->get(string $id): mixed // Resolve the instance (cached)
resources/
Index.php # GET /
Users/
Index.php # GET /users, POST /users
___user_id___/
Index.php # GET /users/{user_id}, PUT /users/{user_id}, DELETE /users/{user_id}
Posts/
Index.php # GET /users/{user_id}/posts
Catchall/
CatchAllResource.php # Fallback match for /catchall/*
resources/
Index.php # GET /
Users/
Index.php # GET /users, POST /users
___user_id___/
Index.php # GET /users/{user_id}, PUT /users/{user_id}, DELETE /users/{user_id}
Posts/
Index.php # GET /users/{user_id}/posts
Catchall/
CatchAllResource.php # 兜底匹配 /catchall/*
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.