PHP code example of muriloamaral / middleware

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/ */

    

muriloamaral / middleware example snippets



return array(
    'modules' => array(
        // ...
        'Middleware',
    ),
    // ...
);


// ...
'middlewares' => array(
    'global' => array(
        'my.first.middleware',
        'my.second.middleware'
    ),
    'local' => array(
        'Application\Controller\IndexController' => array(
            'my.third.middleware'        
        ),
    ),
),
// ...
'service_manager' => array(
    // ...
    'invokables' => array(
        // ...
        'my.first.middleware' => 'Application\Middleware\First',
        'my.second.middleware' => 'Application\Middleware\Second',
        // ...
    ),
    // ...
    'services' => array(
        // ...
        'my.third.middleware' => function($request, $response, $next) {
            // My code here. For instance:

            var_dump($request->getHeader('user-agent'));
            $next();
        },
        // ...
    ),
    // ...
),
// ...


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.
        }
    }
    


    // ...
    'middlewares' => array(
        'global' => array(
            'Application\Middleware\First'
        )
    ),
    // ...
    


    // ...
    'service_manager' => array(
        // ...
        'abstract_factories' => array(
            // ...
            'Middleware\Factory\MiddlewareAbstractServiceFactory'
        ),
        // ...
    ),
    // ...
    


'middlewares' => array(
    'global' => array(
        'my.first.middleware',
        'my.second.middleware',
        'MyNamespace\MyClass::MyStaticMethod', // Static method sample
        function ($request, $response, $next) // Function sample
        {
            var_dump($request->getHeader('user-agent'));
            $next();    
        }
    ),
    'local' => array(
        'Application\Controller\IndexController' => array(
            'my.third.middleware'        
        ),
    ),
),   
bash
$ php composer.phar update
bash
module/Application/config/module.config.php
bash
module/Application/src/Application/Middleware/First.php
bash
module/Application/src/Application/Middleware/Second.php
bash
module/Application/src/Application/Middleware/First.php
bash
    module/Application/src/Application/Middleware/First.php
    
bash
    module/Application/config/module.config.php
    
bash
    module/Application/config/module.config.php