1. Go to this page and download the library: Download php-compatible/router 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/ */
use PhpCompatible\Router\ServerRequest;
use PhpCompatible\Router\JsonResponse;
use PhpCompatible\Router\HtmlResponse;
// Request object
$request = new ServerRequest();
$request->getMethod(); // GET, POST, etc.
$request->getUri(); // /users/123
$request->getQueryParams(); // $_GET
$request->getRouteParam(':id'); // URL parameters
// JSON Response
$response = JsonResponse::ok(array('status' => 'success'));
$response = JsonResponse::created(array('id' => 1));
$response = JsonResponse::badRequest(array('error' => 'Invalid'));
$response = JsonResponse::notFound(array('error' => 'Not found'));
// HTML Response
$response = HtmlResponse::view('/path/to/template.php', array('name' => 'John'));
HTTP_OK // 200
HTTP_CREATED // 201
HTTP_NO_CONTENT // 204
HTTP_BAD_REQUEST // 400
HTTP_UNAUTHORIZED // 401
HTTP_FORBIDDEN // 403
HTTP_NOT_FOUND // 404
HTTP_INTERNAL_SERVER_ERROR // 500
// ... and many more
use PhpCompatible\Router\HttpException;
// Throw exceptions that automatically set status codes
throw HttpException::badRequest('Invalid input');
throw HttpException::unauthorized('Please log in');
throw HttpException::forbidden('Access denied');
throw HttpException::notFound('Resource not found');
throw HttpException::internalServerError('Something went wrong');