https://github.com/php-collective/laravel-dto
https://github.com/php-collective/laravel-dto
Last synced: 5 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/php-collective/laravel-dto
- Owner: php-collective
- License: mit
- Created: 2025-12-15T13:26:52.000Z (6 months ago)
- Default Branch: master
- Last Pushed: 2025-12-16T22:29:08.000Z (6 months ago)
- Last Synced: 2025-12-18T16:39:18.291Z (6 months ago)
- Language: PHP
- Size: 34.2 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Laravel DTO
Laravel integration for [php-collective/dto](https://github.com/php-collective/dto).
## Installation
```bash
composer require php-collective/laravel-dto
```
The service provider will be auto-discovered.
## Configuration
Publish the config file:
```bash
php artisan vendor:publish --provider="PhpCollective\LaravelDto\DtoServiceProvider"
```
This creates `config/dto.php` with the following options:
```php
return [
'config_path' => config_path(), // Where DTO config files are located
'output_path' => app_path('Dto'), // Where to generate DTOs
'namespace' => 'App\\Dto', // Namespace for generated DTOs
];
```
## Usage
### 1. Create your DTO configuration
Create `config/dto.xml` (or `config/dtos.xml` to avoid conflicts):
```xml
```
### 2. Generate DTOs
```bash
php artisan dto:generate
```
Options:
- `--dry-run` - Preview changes without writing files
- `-v` - Verbose output
### 3. Use your DTOs
```php
use App\Dto\UserDto;
$user = new UserDto();
$user->setId(1);
$user->setName('John Doe');
$user->setEmail('john@example.com');
return response()->json($user->toArray());
```
Or create from an array:
```php
$user = UserDto::createFromArray([
'id' => 1,
'name' => 'John Doe',
'email' => 'john@example.com',
]);
```
## Supported Config Formats
The package supports multiple config file formats:
- `dto.xml` or `dtos.xml` - XML format
- `dto.yml` / `dto.yaml` or `dtos.yml` / `dtos.yaml` - YAML format
- `dtos.php` - PHP format (use `dtos.php` to avoid conflict with `config/dto.php`)
- `dto/` subdirectory with multiple files
## License
MIT