PHP code example of orbiter / middleware-utils

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

    

orbiter / middleware-utils example snippets



use Orbiter\MiddlewareUtils\HasResponseFactory;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;

class SampleMiddleware implements MiddlewareInterface {
    use HasResponseFactory;

    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface {
        if($request->hasHeader('error') === 'some-strange-bug') {
            // Simple Status Response
            return $this->create500();
            // Same like `create500`
            return $this->createResponse(500, 'Internal Server Error');
        }

        if($request->hasHeader('error') === 'client-error') {
            // Creating: 400 Bad Request with JSON Body
            return $this->respondJson($this->create400(), ['message' => 'client-error-msg']);
        }
        
        return $handler->handle($request);
    }
}


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

use Orbiter\MiddlewareUtils\HasResponseFactory;
use Orbiter\MiddlewareUtils\ApiError;

class SampleMiddleware implements MiddlewareInterface {
    use HasResponseFactory;

    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface {
        if($request->hasHeader('error') === 'user-not-found') {
            // Creating: 400 Bad Request with ApiError as JSON Body
            return $this->respondJson($this->create404(), new ApiError('User Not Found'));
        }
        
        return $handler->handle($request);
    }
}


use Orbiter\MiddlewareUtils\CorsMiddleware;

$pipe = new RespondPipe();

/**
 * @var DI\FactoryInterface $factory
 */
$pipe->with(
    $factory->make(CorsMiddleware::class, [
        'origins_allowed' => [
            'http://localhost:3000',
            'https://admin.example.org',
        ],
        'headers_allowed' => [
            'Content-Type',
            'Accept',
            'AUTHORIZATION',
            'X-Requested-With',
            'X_AUTH_TOKEN',
            'X_AUTH_SIGNATURE',
            'X_API_OPTION',
            'remember-me',
        ],
        'headers_expose' => [
            'Content-Range',
        ],
        'max_age' => 2,
    ])
);


use Zend\Stratigility\MiddlewarePipe;

use DI\FactoryInterface;
use Orbiter\MiddlewareUtils\CorsMiddleware;

class PipelineFactory
{
    public function __invoke(FactoryInterface $factory)
    {
        $pipeline = new MiddlewarePipe();

        // create CORS Middleware with PHP-DI
        $pipeline->pipe($factory->make(CorsMiddleware::class, [
            'origins_allowed' => ['http://localhost:3000'],
            'headers_allowed' => [
                'Content-Type',
                'Accept',
                'AUTHORIZATION',
                'X-Requested-With',
                'X_AUTH_TOKEN',
                'X_AUTH_SIGNATURE',
                'X_API_OPTION',
                'remember-me',
            ],
            'headers_expose' => [
                'Content-Range',
            ],
            'max_age' => 2,
        ]));

        $pipeline->pipe(OtherMiddleware::class);
        // ...

        return $pipeline;
    }
}