1. Go to this page and download the library: Download torugo/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/ */
torugo / router example snippets
use Torugo\Router\Attributes\Request\Controller;
use Torugo\Router\Attributes\Request\Get;
#[Controller("/users")]
class UsersController {
#[Get()] // endpoint: GET /users
public function findAll(): array
{
return [/* list of users */];
}
#[Get("/{id}")] // endpoint: GET /users/<the user id>
public function findOne(string $id): array
{
return [/* user data */];
}
}
$uri = Request::getUri();
$requestMethod = Request::getMethod();
// Here you can filter the uri
// The request method must be a member of RequestMethod Enum
try {
$router->resolve($uri, $requestMethod);
} catch (\Throwable $th) {
// Handle errors
}
use Torugo\Router\Router;
$router = new Router;
$router->setPrefix("/v1");
/**
* If you receive a GET request with the URI '/v1/users/12345', the router will execute '/users/12345'
*/
use Torugo\Router\Attributes\Request\Controller;
use Torugo\Router\Attributes\Request\Post;
use Torugo\Router\Request;
#[Controller("/users")]
class UsersController {
#[Post()]
public function add(): array
{
$userData = Request::getData();
/* ADD NEW USER DATA ON DATABASE */
return [/* new user data */];
}
}
declare(strict_types=1);
namespace MyApi\Modules\Users;
use Torugo\Router\Attributes\Request\Controller;
use Torugo\Router\Attributes\Request\Delete;
use Torugo\Router\Attributes\Request\Get;
use Torugo\Router\Attributes\Request\Post;
use Torugo\Router\Attributes\Request\Put;
#[Controller("/users")]
class UsersController {
#[Get()] // endpoint: GET /users
public function findAll(): array
{
return [/* list of users */];
}
#[Get("/{id}")] // endpoint: GET /users/<the user id>
public function findOne(string $id): array
{
return [/* user data */];
}
#[Delete("/{id}")] // endpoint: DELETE /users/<the user id>
public function deleteUser(string $id): string
{
return "Deleted user with $id";
}
#[Post()] // endpoint: POST /users
public function addUser(): array
{
return [/* new user data */];
}
#[Put("/{id}")] // endpoint: PUT /users/<the user id>
public function updateUser(string $id): array
{
return [/* updated user data */];
}
}
declare(strict_types=1);
use MyApi\Modules\Users\UsersController;
use Torugo\Router\Router;
es\Users\UsersController');
try {
$router->autoResolve();
} catch (\Throwable $th) {
http_response_code(400);
echo $th->getMessage();
}