PHP code example of blackbonjour / slim-route-registry

1. Go to this page and download the library: Download blackbonjour/slim-route-registry 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/ */

    

blackbonjour / slim-route-registry example snippets




namespace App\Handlers;

use BlackBonjour\SlimRouteRegistry\Route;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;

#[Route('GET', '/hello')]
class HelloHandler
{
    public function __invoke(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface
    {
        $response->getBody()->write('Hello, World!');

        return $response;
    }
}



use BlackBonjour\SlimRouteRegistry\RouteRegistry;
use Slim\Factory\AppFactory;

o scan for route handlers
// By default, it uses ComposerClassProvider to find classes in the specified paths
$routeRegistry = new RouteRegistry([__DIR__ . '/src/Handlers']);

// Register all routes from the specified paths
$routeRegistry->register($app);

// Run the app
$app->run();

#[Route('GET', '/user/{id}', 'user-profile')]
class UserProfileHandler
{
    // ...
}

#[Route(['GET', 'POST'], '/form')]
class FormHandler
{
    // ...
}

class UserHandler
{
    #[Route('GET', '/users')]
    public function listUsers(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface
    {
        // List users
        return $response;
    }

    #[Route('GET', '/users/{id}')]
    public function getUser(ServerRequestInterface $request, ResponseInterface $response, array $args): ResponseInterface
    {
        // Get specific user
        return $response;
    }
}

#[Route('GET', '/api/users', 'users-list', arguments: ['version' => 'v1', 'cache' => true])]
class UsersListHandler
{
    // ...
}

#[Route('GET', '/secure', 'secure-area', middlewares: [AuthMiddleware::class])]
class SecureAreaHandler
{
    // ...
}

#[Redirect('/old-path', '/new-path')]
#[Route('GET', '/new-path')]
class NewPathHandler
{
    // ...
}

#[Redirect('/legacy', '/modern', 301)]

#[Redirect('/old-path')]
#[Route('GET', '/new-path')]
class NewPathHandler
{
    // ...
}

public function __construct(array $paths, ClassProviderInterface $classProvider = new ComposerClassProvider())

public function register(App $app): void

public function provideClasses(string $path): array

public function __construct(ClassMapGenerator $classMapGenerator = new ClassMapGenerator())

public function provideClasses(string $path): array

#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_METHOD | Attribute::IS_REPEATABLE)]
public function __construct(
    array|string $methods,
    string $path,
    ?string $name = null,
    array $arguments = [],
    array $middlewares = [],
    array $redirects = []
)

#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_METHOD | Attribute::IS_REPEATABLE)]
public function __construct(
    string $from,
    ?string $to = null,
    int $status = 302
)