I'm trying to apply Clean Architecture from uncle Bob in Laravel application.
What i'm concerning is: As uncle Bob describe, the Controller should belongs to third circle: Interface Adapters (from inside-out). It means the Controller only depends on Use Case Circle (2nd), and should not know anything about the framework in 4th circle.
But controller in some frameworks has to extends a base class (for example, an AbstractController class), It also needs to receive an Request object and sometimes return a Response object, so this kinda break the dependency rule of Clean Architecture, as it knows about the framework in the outer circle.
Do i misunderstand? If not is there any solution to not break the dependency rule?
My controller is looking like this:
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use User\UseCase\FetchUsers;
use User\UseCase\FetchUsersRequest;
class UserController extends Controller
{
public function index(Request $request, FetchUsers $fetchUsersUseCase)
{
$useCaseRequest = new FetchUsersRequest(
// extract data from Request
);
$useCaseResponse = $fetchUsersUseCase->handle($useCaseRequest);
return [
'users' => $useCaseResponse->users,
];
}
}
AbstractController
is belong to the third circle. So you don't break any dependency. And if you have data transfer objects(DTO) at the Use case circle for transmit data to the third circle you don't break any dependency.
In order to make this happen you should create DTOs for all requests and responses, map your entities to DTOs and share DTOs instead of entities.
For example: you have an User
entity with a string variable named Name
. You have a controller that is going to fetch user from the use-cases
circle.
Solution: Create a DTO named UserDto
with a string variable(you can call it Name
). Controller knows the UserDto
but not the User entity
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With