Transporters is a name chosen by Apiato for DTO's (Data Transfer Objects). Transporters are used to pass user data (coming from Requests, Commands, or other places) from place to another (Actions to Tasks / Controller to Action / Command to Action / ...).
They are very useful for reducing the number of parameters in functions, which prevents the duplication of the long parameters.
- All Models MUST extend from
Usage from Controller
Normally you would use it like this
Since this example above has some required data, that data must be sent to the constructor:
Creating a Transporter from Test
Here where the magic happens:
If you want to directly transform a
Request to a
Transporter you can simply call
This method does take the
protected $transporter of the
Request class into account. If none is defined, a regular
DataTransporter will be created.
$transporter will now have all fields from
$request - so you can directly access them. In order to do so,
you can call:
Of course, you can also "sanitize" the data, like you would have done in the
Request classes by using
Finally, if you need to access the original
Request object, you can access it via
You can set data in many ways
If the data is defined as required like this on the Transporter:
Then can set data on the Transporter like this:
Passing Objects does not work!, because the third party package cannot hydrate it. So to pass instances from a place to another on the Transporter object, you can do the following:
Warning: you can set instances, but they do not appear when calling
toArray() or other similar functions, since
they cannot be hydrated. See below how you can get the instance form the Transporter object.
To get all data from the Transporter you can call
$data->toJson()... there are many other functions on the class.
To get specific data just call the data name, as you would when accessing data from a Request object