Download the PHP package fesor/request-objects without Composer

On this page you can find all versions of the php package fesor/request-objects. 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-objects

Symfony Request Objects

Build Status Scrutinizer Code Quality Latest Stable Version Total Downloads License

Note: This library should not be considered as production ready until 1.0 release. Please provide your feedback to make it happen!

Why?

Symfony Forms component is a very powerful tool for handling forms. But nowadays things have changed. Complex forms are handled mostly on the client side. As for simple forms symfony/forms has very large overhead.

And in some cases you just don't have forms. For example, if you are developing an HTTP API, you probably just need to interact with request payload. So why not just wrap request payload in some user defined object and validate just it? This also encourages separation of concerns and will help you in case of API versioning.

Usage

First of all, we need to install this package via composer:

And register the bundle:

Bundle doesn't require any additional configuration, but you could also specify an error response provider service in bundle config. We will come back to this in "Handle validation errors" section.

Define your request objects

All user defined requests should extend Fesor\RequestObject\RequestObject. Let's create a simple request object for user registration action:

After that we can just use it in our action:

This bundle will bind validated request object to the $request argument. Request object has very simple interface for data interaction. It is very similar to Symfony's request object but is considered immutable by default (although you can add some setters if you wish so)

Where payload comes from?

This library has default implementation of PayloadResolver interface, which acts this way:

1) If a request can have a body (i.e. it is POST, PUT, PATCH or whatever request with body) it uses union of $request->request->all() and $request->files->all() arrays as payload.

2) If request can't have a body (i.e. GET, HEAD verbs), then it uses $request->query->all().

If you wish to apply custom logic for payload extraction, you could implement PayloadResolver interface within your request object:

This will allow you to do some crazy stuff with your requests and DRY a lot of stuff.

Validating payload

As you can see from previous example, the rules method should return validation rules for symfony validator. Your request payload will be validated against it and you will get valid data in your action.

If you have some validation rules which depend on payload data, then you can handle it via validation groups.

Please note: due to limitations in Collection constraint validator it is not so handy to use groups. So instead it is recommended to use Callback validator in tricky cases with dependencies on payload data. See example for details about problem.

You may provide validation group by implementing validationGroup method:

Handling validation errors

If validated data is invalid, library will throw exception which will contain validation errors and request object.

But if you don't want to handle it via kernel.exception listener, you have several options.

First is to use your controller action to handle errors:

But this not so handy and will break DRY if you just need to return common error response. Thats why library provides you ErrorResponseProvider interface. You can implement it in your request object and move this code to getErrorResponse method:

More examples

If you're still not sure is it useful for you, please see the examples directory for more use cases. Didn't find your case? Then share your use case in issues!

Contribution

Feel free to give feedback and feature requests or post issues. PR's are welcomed!


All versions of request-objects with dependencies

PHP Build Version
Package Version
Requires php Version >=7.1.0
symfony/http-foundation Version ^3.3|^4.0
symfony/validator Version ^3.3|^4.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 fesor/request-objects contains the following files

Loading the files please wait ....