Download the PHP package mpscholten/request-parser without Composer

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

request parser

Latest Stable Version License Circle CI Total Downloads

Small PHP Library for type-safe input handling.

The Problem

Let's say you have an action which lists some entities. This includes paging, ascending or descending ordering and optional filtering by the time the entity was created. This action will have some kind of input parsing, which can look like this:

Obviously this code is not very nice to read because it is not very descriptive. It's also pretty verbose for what it's doing. And when you don't pay close attention you will probably miss a null check or a type check.

Now compare the above code to this version:

That's what this library offers. It allows you to express "this action requires a page parameter of type int" or "this action has an optional parameter createdAt of type DateTime, and will be set to a default value if absent".

Examples

If you'd like to go straight to the code now, you can just play around with the examples.

  1. cd /tmp
  2. git clone [email protected]:mpscholten/request-parser.git
  3. cd request-parser
  4. composer install
  5. cd examples
  6. php -S localhost:8080
  7. Open it: http://localhost:8080/symfony.php?action=hello

There are also several other php files inside the examples directory. To get your hands dirty, I suggest just modifying the examples a bit.

Getting Started

Install via composer

Integrations

Symfony HttpFoundation

The following example assumes you're using the symfony Request:

Then you can use the library like this:

When doing GET /MyController/myAction?someParameter=example, the $someParameter variable will contain the string "example".

You might wonder what happens when we leave out the ?someParameter part, like GET /MyController/myAction. In this case the $this->queryParameter('someParameter')->string()->required() will throw a NotFoundException. This exception can be handled by your application to show an error message.

Take a look at the examples.

Psr7

The following example assumes you're using the Psr7 ServerRequestInterface:

Then you can use the library like this:

When doing GET /MyController/myAction?someParameter=example, the $someParameter variable will contain the string "example".

You might wonder what happens when we leave out the ?someParameter part, like GET /MyController/myAction. In this case the $this->queryParameter('someParameter')->string()->required() will throw a NotFoundException. This exception can be handled by your application to show an error message.

Take a look at the examples.

Optional Parameters

To make the someParameter optional, we can just replace required() with defaultsTo($someDefaultValue):

When doing GET /MyController/myAction, the $someParameter variable will now contain the string "no value given". No exception will be thrown because we specified a default value.

In general you first specify the parameter name, followed by the type and then specify whether the parameter is required or is optional with a default value.

For more examples, check out the examples/ directory of this repository. It contains several runnable examples.

Integers, Enums, DateTimes and Json Payloads

Often we need more than just strings. RequestParser also provides methods for other data types:

All of these types also provide a defaultsTo variant.

Supported Data Types
Type Code example Input example
String $this->queryParameter('name')->string()->required(); 'John Doe'
Comma-Separated String $this->queryParameter('names')->commaSeparated()->string()->required(); 'John Doe,John Oliver'
Integer $this->queryParameter('id')->int()->required(); '5'
Comma-Separated Integer $this->queryParameter('groupIds')->commaSeparated()->int()->required(); '5,6,7,8'
Float $this->queryParameter('ratio')->float()->required(); '0.98'
Comma-Separated Float $this->queryParameter('precipitation')->commaSeparated()->float()->required(); '0.98,1.24,5.21'
DateTime $this->queryParameter('timestamp')->dateTime()->required(); '2016-07-20'
Comma-Separated DateTime $this->queryParameter('eventTimes')->commaSeparated()->dateTime()->required(); '2016-07-20 13:10:50,2016-07-21 12:01:07'
Boolean $this->queryParameter('success')->boolean()->required(); 'true'
Comma-Separated Boolean $this->queryParameter('answers')->commaSeparated()->boolean()->required(); '1,0,0,1'
Yes/No Boolean $this->queryParameter('success')->yesNoBoolean()->required(); 'yes'
Comma-Separated Yes/No Boolean $this->queryParameter('answers')->commaSeparated()->yesNoBoolean()->required(); 'y,n,n,y,n'
JSON $this->queryParameter('payload')->json()->required(); '{"event":"click","timestamp":"2016-07-20 13:10:50"}'
Comma-Separated JSON $this->queryParameter('events')->commaSeparated()->json()->required(); '{"event":"click","timestamp":"2016-07-20 13:10:50"},{"event":"add_to_basket","timestamp":"2016-07-20 13:11:01"}'
GET Requests:

$this->queryParameter($name) tells the controller that we want a query parameter (everything after the "?" is called the query string). This is usually what we want when dealing with GET requests

POST Requests:

When we're dealing with a POST request, we need to use $this->bodyParameter($name) to access form fields or the ajax payload.

Autocompletion

The library allows you to take extensive use of autocompletion features of your IDE. E.g. after typing $this->queryParameter('someParameter)-> your IDE will offer you all the possible input types, e.g. string() or int(). After picking a type, e.g. string(), your IDE will offer required() or defaultsTo(defaultValue) to specify the behavior when the parameter is not set.

Static Analysis

The library supports static analysis by your IDE. E.g. when having a parameter like $createdAt = $this->queryParameter('createdAt')->dateTime()->required();, your IDE will know that $createdAt is a DateTime object. This allows you to detect type errors while editing and also decreases the maintenance cost of an action because the types improve legibility.

The library also decreases the risk of unexpected null values because parameters always have an explicit default value or are required.

Error Handling

When a parameter is required but not found or when validation fails, the library will throw an exception. The default exceptions are \MPScholten\RequestParser\NotFoundException and \MPScholten\RequestParser\InvalidValueException. The suggested way to handle the errors thrown by the library is to catch them inside your front controller:

Using Custom Exception Classes

Using Custom Exception Messages

Overriding Single Messages

If you need to override the exception message thrown by the library just once or twice, you can do this by passing the exception messages as the first and second argument to ->required():

Overriding All Messages

If you don't want to specify a custom exception message for all your actions, but still don't want to use the built-in exception messages, you can provide your own exception message generator:

Check it out this example about custom exceptions.

Is It Production Ready?

Absolutely. This library was initially developed at quintly and is extensively used in production since april 2015. Using it at scale in production means there's a a big focus on backwards compatibility and not breaking stuff.

Tests

Contributing

Feel free to send pull requests!


All versions of request-parser with dependencies

PHP Build Version
Package Version
No informations.
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 mpscholten/request-parser contains the following files

Loading the files please wait ....