Download the PHP package hosseinkalateh/simple-dto without Composer
On this page you can find all versions of the php package hosseinkalateh/simple-dto. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download hosseinkalateh/simple-dto
More information about hosseinkalateh/simple-dto
Files in hosseinkalateh/simple-dto
Package simple-dto
Short Description Data Transfer Objects in a simple way for Laravel applications
License MIT
Informations about the package simple-dto
Simple-Dto
DTOs (Data Transfer Objects) are objects that help facilitate the transfer of data between application layers, ensuring a structured and predictable format for the data being passed around.
Why Use DTOs?
Data Transfer Objects improve the separation of concerns by isolating data representation from the core business logic, making your code more maintainable and robust. They act as a layer between controllers, services, and models, providing more control over the data structure and improving security by limiting exposed data fields.
Features
- Simplifies the creation and management of DTOs in Laravel.
- Enforces strict typing and validation of data.
- Integrates seamlessly into your Laravel projects without deviating from the framework's core conventions.
- Allowing form request validation fields to be derived automatically from the DTO properties, ensuring consistency and reducing duplication.
Installation
Usage
Creating a DTO
To create a new DTO class, you can use the following artisan command
This command will generate a new DTO class file in the app/DTO directory, ready for you to define the data structure.
You can define typed properties in your DTO outside the constructor:
Remember that the property name convention can be whatever you want.
Creating a Form Request
To create a new Form Request class, you can use the following artisan command
In this command the --dto specifies the corresponding dto.
NOTE : --dto is required.
This command will generate a From Request file in the appropriate directory with the properties defined in the dto.
Using DTO
Now in the controller, you can access the request and the DTO also.
NOTE : $request->toDto() will return the DTO instance that we passed to the make request command.