PHP code example of webignition / encapsulating-request-resolver-bundle

1. Go to this page and download the library: Download webignition/encapsulating-request-resolver-bundle 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/ */

    

webignition / encapsulating-request-resolver-bundle example snippets


# config/bundles.php
return [
    //...
    webignition\EncapsulatingRequestResolverBundle\EncapsulatingRequestResolverBundle::class => ['all' => true],
];


# src/Controller/UserController
namespace App\Controller;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class UserController 
{
    public function create(Request $request): Response
    {
        $email = (string) $request->request->get('email');
        $password = (string) $request->request->get('password');

        // ... pass $email and $password to relevant services, build and return a response
    }
}

# src/Request/CreateUserRequest.php

namespace App\Request;

use Symfony\Component\HttpFoundation\Request;
use webignition\EncapsulatingRequestResolverBundle\Model\EncapsulatingRequestInterface;

class CreateUserRequest implements EncapsulatingRequestInterface
{
    public function __construct(private string $email, private string $password)
    {
    }

    public static function create(Request $request): CreateUserRequest
    {
        return new CreateUserRequest(
            (string) $request->request->get('email'),
            (string) $request->request->get('password')
        );
    }

    public function getEmail(): string
    {
        return $this->email;
    }

    public function getPassword(): string
    {
        return $this->password;
    }
}

# src/Controller/UserController
namespace App\Controller;

use App\Request\CreateUserRequest;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class UserController 
{
    public function create(CreateUserRequest $request): Response
    {     
        $email = $request->getEmail();
        $password = $request->getPassword());

        // ... pass $email and $password to relevant services, build and return a response      
    }
}