onramplab/laravel-clean-architecture is a Laravel package for an composer template.
It currently has 2 GitHub stars and 25.238 downloads on Packagist (latest version 12.0.0).
Install it with composer require onramplab/laravel-clean-architecture.
Discover more Laravel packages by onramplab
or browse all Laravel packages to compare alternatives.
Last updated
Package to simply setup a Clean Architecture Application in Laravel.
composer require onramplab/laravel-clean-architecture
The UseCase class combines both DTO and business logic into single class in order to reduce the number of files since the DTO usually is tight to a specific use case. Here are the features when creating your own UseCase classes:
By using our exception handler, you will have better logging and API error response (We follows JSON API Spec). It will include more contexts.
Here is the example of api error response:
{
"errors": [
{
"title": "Fake Domain Exception",
"detail": "A fake message",
"status": 400
}
]
}
Here is the example of error log context:
{
"detail": "A fake message",
"adapter": {
"type": "API",
"route": "test-route",
"method": "GET",
"url": "http://localhost/test-route",
"input": []
},
"errors": [
{
"title": "Unable To Do Something",
"detail": "A fake message",
"exception_class": "OnrampLab\\CleanArchitecture\\Exceptions\\UseCaseException",
"stacktrace": [
"## /var/www/html/tests/Unit/Exceptions/HandlerTest.php(149)",
"#0 /var/www/html/vendor/phpunit/phpunit/src/Framework/TestCase.php(1548): OnrampLab\\CleanArchitecture\\Tests\\Unit\\Exceptions\\HandlerTest->handleUseCaseException2()"
]
},
{
"title": "Fake Domain Exception",
"detail": "A fake message",
"exception_class": "OnrampLab\\CleanArchitecture\\Tests\\Unit\\Exceptions\\FakeDomainException",
"stacktrace": [
"## /var/www/html/tests/Unit/Exceptions/HandlerTest.php(146)",
"#0 /var/www/html/vendor/phpunit/phpunit/src/Framework/TestCase.php(1548): OnrampLab\\CleanArchitecture\\Tests\\Unit\\Exceptions\\HandlerTest->handleUseCaseException2()"
]
}
]
}
GeneralException
DomainExceptionCustomDomainExceptionUseCaseExceptionInternalServerExceptionnamespace App\UseCases;
use OnrampLab\CleanArchitecture\UseCase;
use Spatie\LaravelData\Attributes\Validation\Url;
class DoSomethingUseCase extends UseCase
{
#[Url()]
public string $url;
public function handle(): string
{
return 'do something';
}
}
And then in your controller:
use App\Http\Controllers\Controller;
use App\UseCases\DoSomethingUseCase;
use Illuminate\Http\Request;
class DoSomethingController extends Controller
{
public function doSomething(Request $request)
{
$result = DoSomethingUseCase::perform([
'url' => $request->input('url'),
]);
return response()->json([
'result' => $result,
]);
}
}
Make your Laravel default handler to extend our Handler. For example:
namespace App\Exceptions;
use OnrampLab\LaravelExceptions\Handler as ExceptionHandler;
class Handler extends ExceptionHandler
{
// ...
}
You can follow the document in Creating your validation attribute.
php vendor/bin/phpunit
or
composer test
php vendor/bin/phpcs --standard=PSR2 src/
or
composer psr2check
composer psr2autofix
composer insights:fix
rector:fix
php vendor/bin/phpdoc -d "src" -t "docs"
or
composer docs
To keep track, please refer to CHANGELOG.md.
Also please refer to CONTRIBUTION.md.
This will create a basic project structure for you:
Please refer to LICENSE.