PHP code example of mops1k / request-object-resolver-bundle

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

    

mops1k / request-object-resolver-bundle example snippets




use RequestObjectResolverBundle\Attribute\Query;
use RequestObjectResolverBundle\Attribute\Path;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Validator\Constraints as Assert;

class ExampleRequest
{
    #[Assert\NotNull]
    #[Assert\GreaterThan(0)]
    public ?int $id = null;
    
    #[Assert\NotNull]
    #[Assert\NotBlank]
    public ?string $name = null;
}

/**
 * Request path example: /25?name=Julian
 */
class ExampleController extends AbstractController
{
    #[Route('/{id}', methods: [Request::METHOD_GET])]
    public function __invoke(#[Query, Path] ExampleRequest $exampleRequest): JsonResponse
    {
        // some logic with $exampleRequest
        
        return new JsonResponse([
            'id' => $exampleRequest->id,
            'name' => $exampleRequest->name,
        ]);
    }
}



use RequestObjectResolverBundle\Attribute\Query;
use RequestObjectResolverBundle\Attribute\Path;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Validator\Constraints as Assert;

class ExampleRequest
{
    #[Assert\NotNull]
    #[Assert\GreaterThan(0)]
    public ?int $id = null;
    
    #[Assert\NotNull]
    #[Assert\NotBlank]
    public ?string $title = null;
}

/**
 * Request path example: /25?name=Julian
 */
class ExampleController extends AbstractController
{
    #[Route('/{id}', methods: [Request::METHOD_GET])]
    public function __invoke(#[Query(map: ['name' => 'title']), Path] ExampleRequest $exampleRequest): JsonResponse
    {
        // some logic with $exampleRequest
        
        return new JsonResponse([
            'id' => $exampleRequest->id,
            'title' => $exampleRequest->name,
        ]);
    }
}



use RequestObjectResolverBundle\Attribute\Query;
use RequestObjectResolverBundle\Attribute\Path;
use RequestObjectResolverBundle\Attribute\SkipValidation;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Validator\Constraints as Assert;

class ExampleRequest
{
    #[Assert\NotNull]
    #[Assert\GreaterThan(0)]
    public ?int $id = null;
    
    #[Assert\NotNull]
    #[Assert\NotBlank]
    public ?string $name = null;
}

/**
 * Request path example: /-1?name=
 */
class ExampleController extends AbstractController
{
    #[Route('/{id}', methods: [Request::METHOD_GET])]
    public function __invoke(#[Query, Path, SkipValidation] ExampleRequest $exampleRequest): JsonResponse
    {
        // some logic with $exampleRequest
        
        return new JsonResponse([
            'id' => $exampleRequest->id,
            'name' => $exampleRequest->name,
        ]);
    }
}



use RequestObjectResolverBundle\Attribute\Query;
use RequestObjectResolverBundle\Attribute\Path;
use RequestObjectResolverBundle\Attribute\ValidationGroups;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Validator\Constraints as Assert;

class ExampleRequest
{
    #[Assert\NotNull]
    #[Assert\GreaterThan(0, groups: ['default'])]
    public ?int $id = null;
    
    #[Assert\NotNull]
    #[Assert\NotBlank]
    public ?string $name = null;
}

/**
 * Request path example: /25?name=Julian
 */
class ExampleController extends AbstractController
{
    #[Route('/{id}', methods: [Request::METHOD_POST])]
    public function __invoke(#[Query, Path, ValidationGroups(groups: 'default')] ExampleRequest $exampleRequest): JsonResponse
    {
        // some logic with $exampleRequest
        
        return new JsonResponse([
            'id' => $exampleRequest->id,
            'name' => $exampleRequest->name,
        ]);
    }
}



use RequestObjectResolverBundle\Attribute\Query;
use RequestObjectResolverBundle\Attribute\Path;
use RequestObjectResolverBundle\Attribute\ValidationGroups;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;

class ExampleRequest
{
    #[Assert\NotNull]
    #[Assert\GreaterThan(0)]
    #[Groups(['default'])]
    public ?int $id = null;
    
    #[Assert\NotNull]
    #[Assert\NotBlank]
    public ?string $name = null;
}

/**
 * Request path example: /25?name=Julian
 */
class ExampleController extends AbstractController
{
    #[Route('/{id}', methods: [Request::METHOD_POST])]
    public function __invoke(#[Query(serializerContext: ['groups': ['default']]), Path] ExampleRequest $exampleRequest): JsonResponse
    {
        // some logic with $exampleRequest
        
        return new JsonResponse([
            'id' => $exampleRequest->id,
            'name' => $exampleRequest->name, // will throw error as uninitialized property
        ]);
    }
}



use RequestObjectResolverBundle\Attribute\Query;
use RequestObjectResolverBundle\Attribute\Path;
use RequestObjectResolverBundle\Attribute\SerializerContext;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;

class ExampleRequest
{
    #[Assert\NotNull]
    #[Assert\GreaterThan(0)]
    #[Groups(['default'])]
    public ?int $id = null;
    
    #[Assert\NotNull]
    #[Assert\NotBlank]
    public ?string $name = null;
}

/**
 * Request path example: /25?name=Julian
 */
class ExampleController extends AbstractController
{
    #[Route('/{id}', methods: [Request::METHOD_POST])]
    public function __invoke(#[Query, Path, SerializerContext(['groups': ['default']])] ExampleRequest $exampleRequest): JsonResponse
    {
        // some logic with $exampleRequest
        
        return new JsonResponse([
            'id' => $exampleRequest->id,
            'name' => $exampleRequest->name, // will throw error as uninitialized property
        ]);
    }
}