1. Go to this page and download the library: Download muriloamaral/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/ */
namespace Application\Middleware;
class First
{
public function __invoke($request, $response, $next)
{
// My code here. For instance:
var_dump($request->getHeader('user-agent'));
$next(); // call the next middleware
// Run code after all middlewares run
}
}
namespace Application\Middleware;
class Second
{
public function __invoke($request, $response, $next)
{
// My code here. For instance:
var_dump($request->getHeader('user-agent'));
$next(); // call the next middleware
// Run code after all middlewares run
}
}
namespace Application\Middleware;
use Zend\ServiceManager\ServiceLocatorAwareInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
class First implements ServiceLocatorAwareInterface
{
protected $serviceLocator;
public function __invoke($request, $next, $redirect)
{
// My code here. For instance:
$config = $this->serviceLocator->get('config');
}
public function setServiceLocator(ServiceLocatorInterface $serviceLocator)
{
$this->serviceLocator = $serviceLocator;
}
public function getServiceLocator()
{
return $this->serviceLocator;
}
}
namespace Application\Middleware;
use Closure;
use Zend\Http\PhpEnvironment\Request;
use Zend\Http\PhpEnvironment\Response;
use Middleware\MiddlewareInterface;
class First implements MiddlewareInterface
{
public function __invoke(Request $request, Response $response, Closure $next)
{
// My code here.
}
}