PHPackages                             lsbproject/request-bundle - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. lsbproject/request-bundle

AbandonedArchivedSymfony-bundle[API Development](/categories/api)

lsbproject/request-bundle
=========================

Bundle to structure common request data

v7.2.4(3y ago)12751MITPHPPHP &gt;=5.6

Since Mar 18Pushed 2y ago1 watchersCompare

[ Source](https://github.com/22116/request-bundle)[ Packagist](https://packagist.org/packages/lsbproject/request-bundle)[ RSS](/packages/lsbproject-request-bundle/feed)WikiDiscussions master Synced 1w ago

READMEChangelog (10)Dependencies (4)Versions (42)Used By (1)

LSBProjectRequestBundle
=======================

[](#lsbprojectrequestbundle)

```
Caution: bundle will not have updates in favor of new symfony attribute argument resolvers, consider use it instead of this package

```

[![SymfonyInsight](https://camo.githubusercontent.com/ec40a67ac9d78b9effb08a5f16dfb7d1d543b96549146c54fcdfcc6946b64872/68747470733a2f2f696e73696768742e73796d666f6e792e636f6d2f70726f6a656374732f30653634646132352d323532622d346433662d393735322d3965643138663933633966312f6d696e692e737667)](https://insight.symfony.com/projects/0e64da25-252b-4d3f-9752-9ed18f93c9f1)
Request bundle created to represent request data as a strict objects.

Installation
------------

[](#installation)

Open a command console, enter your project directory and execute the following command to download the latest stable version of this bundle:

```
$ composer require lsbproject/request-bundle

```

Usage
-----

[](#usage)

Create a request class and extend AbstractRequest:

```
use LSBProject\RequestBundle\Request\RequestInterface;

class TestRequest implements RequestInterface
{
    public $fooBar;
}
```

That's all. This will require `foo_bar` parameter to be present in request, query, cookie, head or attribute (All types you can find in `RequestStorage`).
Then use it in controller:

```
/**
 * @Route("/test")
 */
public function test(TestRequest $testRequest): Response
{
    return new Response($testRequest->fooBar);
}
```

### Using objects

[](#using-objects)

Also this bundle supports loading objects like you usually do with `@ParamConverter`. All ParamConverts will be performed to the property.

```
use LSBProject\RequestBundle\Request\RequestInterface;
use App\Model\SomeAwesomeClass;

class TestRequest implements RequestInterface
{
    public SomeAwesomeClass $fooBar;
}
```

If you are not using PHP 7.4, you can point the class with annotations `@var` or `@PropConverter` (attributes also available right now with the same annotation class)

```
use LSBProject\RequestBundle\Configuration\PropConverter;
use LSBProject\RequestBundle\Request\RequestInterface;

class TestRequest implements RequestInterface
{
    /**
     * @var App\Model\SomeAwesomeClass
     */
    public $fooBar;

    // or

    /**
     * @PropConverter(App\Model\SomeAwesomeClass::class)
     */
    public $fooBarBaz;

    // or

    #[PropConverter(App\Model\SomeAwesomeClass::class)]
    public $fooBarBaz;
}
```

(Be aware that you need to specify full classname with a namespace)

### Configuring property

[](#configuring-property)

As you could notice there is a useful annotation `@PropConverter` which is in fact is an adapter to `@ParamConverter` of sensio-framework-bundle. Be free to modify any of parameters, as they are working in the same way as in the original one.

```
use LSBProject\RequestBundle\Request\RequestInterface;
use LSBProject\RequestBundle\Configuration as LSB;

class TestRequest implements RequestInterface
{
    /**
     * @LSB\PropConverter(App\Model\SomeAwesomeClass::class, converter="awesome_converter", options={"mapping": {"user_id": "id"}})
     */
    public $fooBarBaz;
}
```

### Request storage

[](#request-storage)

By default all parameters from body, request, headers, cookies or path will be used.
To restrict parameter to be located in exact place you can use `@RequestStorage` annotation

```
use LSBProject\RequestBundle\Request\RequestInterface;
use LSBProject\RequestBundle\Configuration as LSB;

/**
 * @LSB\RequestStorage({LSB\RequestStorage::BODY, LSB\RequestStorage::PATH})
 */
class TestRequest implements RequestInterface
{
    public $fooBaz;

    /**
     * @LSB\RequestStorage({LSB\RequestStorage::BODY})
     */
    public $fooBar;
}
```

From example above you will get `foo_baz` parameter from request body or path, and `foo_bar` parameter exactly from request body.
There are 5 types of storage: `query`, `body`, `path`, `cookie` and `header`.

### Validation

[](#validation)

You can use `symfony/validation` to validate parameters in request. Install component and use it as usual

```
use LSBProject\RequestBundle\Request\RequestInterface;

class TestRequest implements RequestInterface
{
    /**
     * @Assert\NotBlank()
     */
    public int $userId;
}
```

### Using mutators

[](#using-mutators)

To specify property you also can use setters instead of `public` properties to add some additional logic.

```
use LSBProject\RequestBundle\Request\RequestInterface;

class TestRequest implements RequestInterface
{
    private string $comment;

    public function setComment(string $comment): void
    {
        $this->comment = trim($comment);
    }
}
```

### Working with entities

[](#working-with-entities)

There is an annotation `@Entity` which is almost equal to the sensio annotation.

```
use LSBProject\RequestBundle\Configuration as LSB;
use LSBProject\RequestBundle\Request\RequestInterface;
use App\Entity\User;

class TestRequest implements RequestInterface
{
    /**
     * @LSB\Entity(App\Entity\User::class, expr="repository.find(id)", mapping={"id": "user_id"})
     */
    public $userA;

    // or

    /**
     * @LSB\Entity(options={"id"="user_id"})
     */
    public User $userB;

    // or

    /**
     * @LSB\Entity(options={"mapping": {"user_id": "id"}})
     */
    public User $userC;
}
```

Use `mapping` property to point aliases from the request to the original parameters names.

### Custom naming conversion

[](#custom-naming-conversion)

By default all properties will be converter to snake\_case style. You can change this behaviour by creating a class which implements `LSBProject\RequestBundle\Util\NamingConversion\NamingConversionInterface`

```
