PHP code example of linkeddatacenter / usilex

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

    

linkeddatacenter / usilex example snippets


use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Psr\Http\Server\MiddlewareInterface;

class MyMiddleware implements MiddlewareInterface 
{
    public function process(
        ServerRequestInterface $request, 
        RequestHandlerInterface $handler
        ): ResponseInterface
    {
        //here your code that returns a response or passes the control to the handler
    }
}  


$app = new \uSilex\Application;
$app['uSilex.request'] = \Zend\Diactoros\ServerRequestFactory::fromGlobals();
$app['uSilex.responseEmitter'] = $app->protect(function($response) {echo $response->getBody(); });
$app['uSilex.httpHandler'] = function($app) { 
    return new \Relay\Relay([new MyMiddleware($app)]); 
};
$app->run();

...
$app= new Application;
$app->register( new MyPsr7ServiceProvider() };
$app['my.router'] = function($app) { return new \My\RouterMiddleWare($app) };
$app['my.notfound'] = function($app) { return new \My\NotFoundMiddleWare($app) };
$app['handler.queue'] = [
    'my.router'
    'my.notfound'
];
$app['uSilex.httpHandler'] = function($app) {
   return new MyHttpHandler($app['handler.queue']);
};
$app->boot()->run();


Silex\Application;
use uSilex\ContainerAwareTrait;
use uSilex\Provider\Psr15\RelayServiceProvider as Psr15Provider;
use uSilex\Provider\Psr7\DiactorosServiceProvider as Psr7Provider ;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Psr\Http\Server\MiddlewareInterface;
use Zend\Diactoros\Response\TextResponse;

class MyMiddleware implements MiddlewareInterface {
    use ContainerAwareTrait;
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface {
        return new \Zend\Diactoros\Response\TextResponse( $this->containerGet('message', 'Hi'));
    }
}  

$app = new Application;
$app->register(new Psr15Provider());
$app->register(new Psr7Provider());
$app['myMiddleware'] = function($app) { return new MyMiddleware($app); };
$app['message'] = 'hello world!';
$app['handler.queue'] = ['myMiddleware'];
$app->run();