Download the PHP package macpaw/request-dto-resolver without Composer

On this page you can find all versions of the php package macpaw/request-dto-resolver. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package request-dto-resolver

Symfony Request DTO Resolver Bundle

Automatically resolves and validates Symfony HTTP request data (JSON, form-data, query parameters) into DTOs.

Features

Installation

The bundle should be automatically registered in your config/bundles.php. If not, add it manually:

Configuration

First, define an interface that your DTOs will implement. This allows the resolver to identify which arguments to process.

Then, point the bundle to this interface in a configuration file:

How It Works

The resolver uses a combination of a DTO class and a Symfony Form to process and validate incoming request data.

  1. Controller Argument: You type-hint a controller argument with your DTO class (e.g., UserDto).
  2. FormType Attribute: You decorate the controller action with the #[FormType] attribute, specifying which Symfony Form to use for processing.
  3. Data Resolution: The resolver extracts data from the request based on the form's fields.
  4. Validation: The form validates the data against the constraints defined in your DTO.
  5. DTO Hydration: If validation passes, a new DTO instance is created and populated with the validated data.

Usage

1. Create a DTO

The DTO is a simple PHP class that implements your marker interface. Use Symfony's Validator components to define constraints.

2. Create a Form Type

The Form Type defines the structure of the expected request data and maps it to your DTO.

3. Use in a Controller

In your controller, type-hint the action argument with your DTO class and add the #[FormType] attribute.

Parameter Resolution

The resolver automatically extracts data from the request to populate the form. The source of the data depends on the request's Content-Type header and method.

Resolution Order

For each field defined in your Form Type, the resolver searches for a corresponding value in the following order:

  1. JSON Body: If the request has a Content-Type of application/json, the decoded JSON body is checked first.
  2. Query & Form Data: The resolver then checks request->query (for GET parameters) and request->request (for POST form data).
  3. Request Headers: Finally, it checks the request headers.

This order means that for a POST request with both a JSON body and query parameters, the values in the JSON body will take precedence.

Common Scenarios

Advanced Features

Custom Field Mapping

You can map request fields to different DTO properties using the lookupKey option in your Form Type. This is useful for handling request keys that don't match your DTO property names (e.g., user-id vs. userId).

Form Type Configuration:

This configuration will map the user-id key from any source (JSON body, query, or header) to the userId form field.

Request Example:

Integration with Other Bundles

This bundle is designed to work seamlessly with other bundles that parse the request body (e.g., FOSRestBundle). If the request body is already parsed and populated in $request->request, the resolver will automatically use this pre-parsed data instead of reading the raw body again.

This ensures:

Error Handling

The bundle throws the following exceptions, which you can handle with a standard Symfony exception listener:

Contributing

Feel free to open issues and submit pull requests.

License

This bundle is released under the MIT license.


All versions of request-dto-resolver with dependencies

PHP Build Version
Package Version
Requires php Version >=8.0
symfony/form Version ^6.4|^7.0
symfony/framework-bundle Version ^6.4|^7.0
symfony/serializer Version ^6.4|^7.0
symfony/validator Version ^6.4|^7.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package macpaw/request-dto-resolver contains the following files

Loading the files please wait ....