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;
}
}