Download the PHP package cerbero/laravel-dto without Composer

On this page you can find all versions of the php package cerbero/laravel-dto. 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 laravel-dto

Laravel DTO

Author PHP Version Laravel Version Octane Compatibility Build Status Coverage Status Quality Score Latest Version PSR-12 Total Downloads

Laravel DTO integrates DTO, a package inspired by Lachlan Krautz' excellent data-transfer-object, with the functionalities of Laravel.

A data transfer object (DTO) is an object that carries data between processes. DTO does not have any behaviour except for storage, retrieval, serialization and deserialization of its own data. DTOs are simple objects that should not contain any business logic but rather be used for transferring data.

Below are explained the advantages brought by this package in a Laravel application. In order to discover all the features of DTO, please refer to the full DTO documentation.

Install

Via Composer:

To customize some aspects of this package, the config/dto.php file can optionally be generated via:

Usage

Generate DTOs

DTOs for Eloquent models can be automatically generated by running the following Artisan command:

The database table of the specified model is scanned to populate the DTO properties. Furthermore, if the model has relationships, a DTO is also generated for each related model. For example, if our User model looks like:

The DTOs App\Dtos\UserData and App\Dtos\PostData are generated like so:

By default, DTOs are generated in the Dtos directory which is created where models are. For example the DTO for App\User is generated as App\Dtos\UserData and the DTO for App\Users\User is generated as App\Users\Dtos\UserData.

To change either the location or the suffix Data of generated DTOs, we can create a DTO qualifier by implementing the interface DtoQualifierContract and replace the default qualifier in config/dto.php. The example below qualifies a DTO in the directory of the model and adds the suffix Dto:

Finally, if a model has already its own DTO generated, we can overwrite it with the option --force or -f:

Instantiate a DTO

In addition to the traditional ways to instantiate a DTO, Laravel DTO provides handy methods to create a new instance of DTO from HTTP requests, Eloquent models or other common interfaces present in Laravel.

For example UserData can be instantiated from an HTTP request by calling the method fromRequest():

The request passed to the method fromRequest() is optional: if not provided, the current application request is used to instantiate UserData.

By default the flags PARTIAL and IGNORE_UNKNOWN_PROPERTIES are applied to the DTO when it is instantiated from a request. Additional flags can be passed as second parameter to further customize the behaviour of the DTO.


To instantiate a DTO from an Eloquent model, we can call the method fromModel():

The flags PARTIAL, IGNORE_UNKNOWN_PROPERTIES and CAST_PRIMITIVES are applied to the DTO when it is instantiated from a model. Additional flags can be passed as second parameter.


Finally, the method from() instantiates a DTO from several interfaces (specific to Laravel or not), including:

In this case no flags are applied by default, but they can still be passed as second parameter.

Resolve a DTO

As long as PARTIAL is set in the default flags of a DTO, such DTO can be automatically resolved by the Laravel IoC container with the data carried by the current application request:

Convert into DTO

Another way to get an instance of DTO from different objects is letting them use the trait TurnsIntoDto and call the method toDto():

Classes using the trait can specify the DTO to turn into by:

Flags can optionally be passed as second parameter, or first parameter if the DTO class is already defined in the class using the trait. When models turn into DTOs, the flag CAST_PRIMITIVES is added to help casting values if casts are not defined on the Eloquent models.

Convert into array

By default Laravel DTO registers a value converter for Carbon instances. When a DTO is converted into array, all its Carbon objects are turned into an atom string and then converted back into Carbon instances when a new DTO is instantiated:

Conversions can be added or removed in the config/dto.php file, specifically via the key conversions:

Listen to events

The only feature added by this package to listeners is the ability to resolve dependencies via the Laravel IoC container. Dependencies can be injected into listeners constructor to be automatically resolved.

Listeners can be added or removed in the config/dto.php file, specifically via the key listeners:

Define flags globally

Sometimes we may want all our DTOs to share the same flags, an example might be the need to always work with mutable DTOs. An easy way to accomplish that is defining such flags in the config/dto.php file:

Support for macros

In case we need to add functionalities to all DTOs, an option might be using macros. Please refer to the Laravel documentation to see an example of how to register a macro.

DTO debugging

When using the helpers dump() or dd(), only DTOs data will be shown instead of all the underlying architecture that makes the package work:

Change log

Please see CHANGELOG for more information on what has changed recently.

Testing

Contributing

Please see CODE_OF_CONDUCT for details.

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.


All versions of laravel-dto with dependencies

PHP Build Version
Package Version
Requires php Version ^7.3||^8.0
cerbero/dto Version ^2.1
doctrine/dbal Version ^3.0
illuminate/console Version >=8.0
illuminate/container Version >=8.0
illuminate/database Version >=8.0
illuminate/filesystem Version >=8.0
illuminate/http Version >=8.0
illuminate/support Version >=8.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 cerbero/laravel-dto contains the following files

Loading the files please wait ....