PHP code example of torugo / router

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 */];
    }
}

use Torugo\Router\Router;

$router = new Router;

$router->registerMany([
    UsersController::class,
    CostumersController::class,
    SuppliersController::class,
    StockController::class,
]);

try {
    $router->autoResolve();
} catch (\Throwable $th) {
    // Handle errors
}

$router->register(UsersController::class);
$router->register(CostumersController::class);
$router->register(SuppliersController::class);
$router->register(StockController::class);

try {
    $router->autoResolve();
} catch (\Throwable $th) {
    // Handle errors
}

$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 */];
    }
}

use Torugo\Router\Attributes\Request\Controller;
use Torugo\Router\Attributes\Request\Get;

#[Controller("users")]
class UsersController {
    #[Get()]
    public function findAll(): array
    {
        $users = [
            [
                "id" => "1",
                "name" => "User 1",
                "email" => "[email protected]"
            ],
            [
                "id" => "2",
                "name" => "User 2",
                "email" => "[email protected]"
            ],
            [
                "id" => "3",
                "name" => "User 3",
                "email" => "[email protected]"
            ],
        ];

        return $users;
        // '[{"id":"1","name":"User 1","email":"[email protected]"},{"id":"2","name":"User ... ]'
    }
}

     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();
    }
    
shell
php -S localhost\:8000 -t tests/ServerTests/Server/