PHP code example of archict / router

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

    

archict / router example snippets




use Archict\Brick\Service;
use Archict\Brick\ListeningEvent;
use Archict\Router\RouteCollectorEvent;

#[Service]
class MyService {
    #[ListeningEvent]
    public function routeCollector(RouteCollectorEvent $event) 
    {
    }
}

$event->addRoute(Method::GET, '/hello', static fn() => 'Hello World!');

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;

$event->addRoute(Method::GET, '/hello', new MyController());

class MyController implements RequestHandler {
    public function handle(ServerRequestInterface $request): ResponseInterface
    {
        return ResponseFactory::build()
            ->withStatus(200)
            ->withBody('Hello World!')
            ->get();
    }
}



use Archict\Brick\Service;
use Archict\Brick\ListeningEvent;
use Archict\Router\RouteCollectorEvent;
use Psr\Http\Message\ServerRequestInterface;

#[Service]
class MyService {
    #[ListeningEvent]
    public function routeCollector(RouteCollectorEvent $event) 
    {
        $event->addMiddleware(Method::GET, '/hello', static function(ServerRequestInterface $request): ServerRequestInterface {
            // Do something
            return $request
        });
        // Or
        $event->addMiddleware(Method::GET, '/hello', new MyMiddleware());
    }
}

use Psr\Http\Message\ServerRequestInterface;

class MyMiddleware implements Middleware
{
    public function process(ServerRequestInterface $request): ServerRequestInterface
    {
        return $request;
    }
}



use Archict\Router\ResponseHandler;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;

class MyHandler implements ResponseHandler
{
    public function handleResponse(ResponseInterface $response, ServerRequestInterface $request): ResponseInterface
    {
        $factory = new \GuzzleHttp\Psr7\HttpFactory();
        return $response->withBody($factory->createStream("Page '{$request->getUri()->getPath()}' not found!"));
    }
}