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.

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 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:

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:

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

PHP Build Version
Package Version
Requires php Version ^8.2
illuminate/support Version ^10.0|^11.0
illuminate/http Version ^10.0|^11.0
illuminate/container Version ^10.0|^11.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 serter35/lto contains the following files

Loading the files please wait ....