PHP code example of benycode / slim-middleware

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

    

benycode / slim-middleware example snippets


use BenyCode\Slim\Middleware\HealthCheckEndpointMiddleware;

return [
    ......
    HealthCheckEndpointMiddleware::class => function (ContainerInterface $container) {
        return new HealthCheckEndpointMiddleware(
           [
              'health_endpoint' => '/_health', // change if needed other endpoint
           ],
           <<inject you PSR7 logger if needed>>,
        );
    },
    ......
];

use Slim\Exception\HttpNotFoundException;
use BenyCode\Slim\Middleware\HealthCheckEndpointMiddleware;

$app
   ->get(
   '/{any:.*}',
   function (Request $request, Response $response) {
      throw new HttpNotFoundException($request);
   }
   )
   ....
   ->add(HealthCheckEndpointMiddleware::class)
   ->setName('any')
   ;

use BenyCode\Slim\Middleware\InfoEndpointMiddleware;

return [
    ......
    InfoEndpointMiddleware::class => function (ContainerInterface $container) {
        return new InfoEndpointMiddleware(
           [
              'info_endpoint' => '/_info', // change if needed other endpoint
           ],
           '<<define api version here>>', // example: v0.0.0
        );
    },
    ......
];

use Slim\Exception\HttpNotFoundException;
use BenyCode\Slim\Middleware\InfoEndpointMiddleware;

$app
   ->get(
   '/{any:.*}',
   function (Request $request, Response $response) {
      throw new HttpNotFoundException($request);
   }
   )
   ....
   ->add(InfoEndpointMiddleware::class)
   ->setName('any')
   ;

use BenyCode\Middleware\SettingsUpMiddleware;

return function (App $app) {
        ...
        $app->add(SettingsUpMiddleware::class);
        ...
};

protected function __invoke(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface {
   $settings = $request
      ->getAttribute('settings')
   ;
}

use BenyCode\Middleware\ExceptionMiddleware;

return function (App $app) {
        ...
        $app->add(ExceptionMiddleware::class);
        ...
};

use BenyCode\Slim\Middleware\APISIXRegisterMiddleware;

return [
    ......
    APISIXRegisterMiddleware::class => function (ContainerInterface $container) {
       return new APISIXRegisterMiddleware(
       [
          'register_endpoint' => '/_health', // change if needed other endpoint
          'service_id' => '<<describe your service name>>',
          'service' => [
             'upstream' => [
                'type' => 'roundrobin',
                'nodes' => [
                   '<<describe working endpoint>>:<<describe working port>>' => 1, // example: books-microservice:80
                ],
             ],
          ],
          'route' => [
             'uri' => "<<describe working path>>", // example: /books/*
             'service_id' => '<<describe service id>>', // example: books-microservice
          ],
          'api_admin_secret' => '<<describe APISIX admin secret>>',
          'api_endpoint' => '<<describe APISIX API endpoint url>>', // example: http://api-gateway:9180
        ],
	<<inject you PSR7 logger if needed>>,
        );
    },
    ......
];

use Slim\Exception\HttpNotFoundException;
use BenyCode\Slim\Middleware\APISIXRegisterMiddleware;

$app
   ->get(
   '/{any:.*}',
   function (Request $request, Response $response) {
      throw new HttpNotFoundException($request);
   }
   )
   ....
   ->add(APISIXRegisterMiddleware::class)
   ->setName('any')
   ;

use BenyCode\Slim\Middleware\LeaderElectionMiddleware;

return [
    ......
    LeaderElectionMiddleware::class => function (ContainerInterface $container) {
       return new LeaderElectionMiddleware(
          [
             'leader_election_endpoint' => '/_health', // change if needed other endpoint
             'etcd_endpoint' => '<<etcd endpoint>>',
             'alection_frequency' => 5, // alection frequence in seconds
              <<inject you PSR7 logger if needed>>,
          ],
        );
    },
    ......
];

use Slim\Exception\HttpNotFoundException;
use BenyCode\Slim\Middleware\LeaderElectionMiddleware;

$app
   ->get(
   '/{any:.*}',
   function (Request $request, Response $response) {
      throw new HttpNotFoundException($request);
   }
   )
   ....
   ->add(LeaderElectionMiddleware::class)
   ->setName('any')
   ;

protected function __invoke(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface {
   $leader = $request
      ->getAttribute('im_leader')
   ;

   if($leader) {
      // the leader code
   }
}

use BenyCode\Slim\Middleware\OnePathXApiTokenProtectionMiddleware;

return [
    ......
    OnePathXApiTokenProtectionMiddleware::class => function (ContainerInterface $container) {
       return new OnePathXApiTokenProtectionMiddleware(
          [
             'path' => '/_health', // change if needed other endpoint
             'x-api-token' => '4bfdb81c03f42600d9018103a4df878b', // change to yours
              <<inject you PSR7 logger if needed>>,
          ],
        );
    },
    ......
];

use Slim\Exception\HttpNotFoundException;
use BenyCode\Slim\Middleware\OnePathXApiTokenProtectionMiddleware;

$app
   ->get(
   '/{any:.*}',
   function (Request $request, Response $response) {
      throw new HttpNotFoundException($request);
   }
   )
   ....
   ->add(OnePathXApiTokenProtectionMiddleware::class)
   ->setName('any')
   ;