Download the PHP package serter35/lto without Composer
On this page you can find all versions of the php package serter35/lto. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package lto
LTO (Laravel Transfer Object)
Introduction
LTO helps you structure and manage data transfer in your application. It simplifies handling incoming request data, ensures type safety, and improves code clarity.
By leveraging DTOs, you can enforce a consistent data structure, prevent unexpected values, and seamlessly integrate with Laravelβs validation, models, and dependency injection.
π Why Use LTO?
β Full IDE Auto-Completion Support β Since DTOs use typed properties and constructor parameters, your IDE (PhpStorm, VS Code, etc.) can provide full auto-completion while writing code.
β Strict Type Safety β Prevents unexpected data structures and makes your code more predictable.
β Improved Code Readability β Instead of working with raw request arrays, DTOs give a structured way to handle incoming data.
β Seamless Integration with Laravel β Works natively with request validation, dependency injection, and models.
Installation
You can install LTO via Composer:
LTO supports Laravel 10 and Laravel 11 and requires PHP 8.2 or higher.
Quick Start
First, create a DTO class:
Define the DTO class with the relevant properties.
For example, in the following example, we have created a DTO to store comments:
You can use this DTO class within a controller using Laravel's Method Injection feature:
This way, the #[FromBody]
attribute automatically maps the body
data of the Laravel request to the DTO.
Now, let's take a look at what kind of DTOs we can create for different request types.
Creating a DTO
1. Using Request DTO
You can use the relevant attributes to bind DTOs with Laravel requests. If you want to use DTOs via Dependency Injection in controllers, you must specify an attribute (e.g., #[FromBody]). However, attributes are not mandatory for using DTOsβyou can still instantiate them manually. The package supports the following request sources:
- FromBody β Gets data from the body of the request.
- FromQuery β Gets data from the query string.
- FromRoute β Gets data from route parameters.
- FromRequest β Automatically maps without specifying the source.
a) Using FromBody
b) Using FromQuery
Usage example:
c) Using a Complex DTO
You can gather different request parameters within a single DTO. In the example below, the #[FromBody] attribute is applied to the class itself, meaning that by default, all properties will be mapped from the request body. However, individual properties can override this behavior using different attributes such as #[FromQuery] or #[FromRoute].
d) Using FromRequest
To automatically scan all request sources:
π― Using Binding Keys in Attributes
In addition to the default behavior, attributes allow you to specify a binding key to map request data from a different field name.
For example, if the request body contains a field named "header", but you want to map it to $title in the DTO, you can do:
This tells the package to extract the "header" field from the request body and assign it to $title. You can use this approach to rename request fields dynamically while keeping your DTO structure clean.
2. Creating a Simple DTO (Without Attributes)
Attributes are not required to define a DTO. You can create a DTO as a regular PHP class without using attributes.
However, if a DTO does not have an attribute, Laravel will not be able to automatically inject it via Dependency Injection (DI) in controllers. Instead, you must manually instantiate it.
π Note: Even without attributes, you can still use methods like fromRequest(), fromArray(), and fromModel() to populate the DTO. These methods are explained in the following sections.
Using DTOs
1. Using Dependency Injection (DI)
You can pass DTOs into controller methods via Dependency Injection. This takes advantage of Laravel's automatic dependency resolution feature.
For details on how DTOs are created, see the Creating a DTO section.
2. Instantiating DTOs Manually
You can manually create a DTO instance using the new keyword. This approach is useful when Dependency Injection is not an option, or when you need to create a DTO dynamically.
2. Using FromRequest
DTOs can be used to directly retrieve data from the incoming request object. This is a common approach to integrate request validation and data processing into the DTO.
3. Using FromModel
When retrieving data from a model, you can convert the DTO to a model instance. This is ideal for using model data in a specific format.
4. Using FromArray
This is used to convert an array of data into a DTO. It's a common way to work with external data sources.
Converting DTOs
You can convert a DTO to different formats using the following methods:
- toArray() β Converts to an array.
- toCollection() β Converts to a Laravel Collection.
- toModel() β Converts to a model instance.
Examples:
DTOs with Validation Support
1. Defining a Validatable DTO
To create a Validatable DTO class via the console, you can run the following command:
To add validation support to an existing DTO class, you can use the Validatable
trait:
To customize validation error messages:
2. Using DTO Validation
To get only the validated data:
All versions of lto with dependencies
illuminate/support Version ^10.0|^11.0
illuminate/http Version ^10.0|^11.0
illuminate/container Version ^10.0|^11.0