PHP code example of zholus / symfony-middleware

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

    

zholus / symfony-middleware example snippets




namespace App\Middleware;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Zholus\SymfonyMiddleware\MiddlewareInterface;
use App\Repository\UserRepository;

final class AdminAccessMiddleware implements MiddlewareInterface
{
    private $userRepository;
    
    public function __construct(UserRepository $userRepository)
    {
        $this->userRepository = $userRepository;
    }
    
    public function handle(Request $request): ?Response
    {
        $login = $request->get('login');
        $password = $request->get('password');
        
        $user = $this->userRepository->findByCredentials($login, $password);
        
        if ($user === null || !$user->isAdmin()) {
            return new Response('Denied access', 403);
        }
        
        return null;
    }
}