1

I'm confused about why we need constructor and can someone explain me about this code from my controller :

    public function __construct(MerchantService $merchantService,    PaymentService $paymentService){
    $this->merchantService = $merchantService;
    $this->paymentService = $paymentService;
}

I am working on an admin panel with laravel . and our boss wants the stucture to be like this:

controller -> service -> repository -> modal -> database

it's pretty straight forward when i am going this route:

controller -> modal ->database.

but i have to follow the first one. The above code is a sample from the controller

in the above code there are 2 services, MerchantService and PaymentService. but i do not understand what exactly is the constructor doing with the Merchant service variable and payment variable as params, is it initiating an object of Merchant service and PaymentService??

Niharika
  • 1,188
  • 15
  • 37
Manas
  • 3,060
  • 4
  • 27
  • 55

2 Answers2

1

The Laravel service container is a powerful tool for managing class dependencies and performing dependency injection. Dependency injection is a fancy phrase that essentially means this: class dependencies are "injected" into the class via the constructor or, in some cases, "setter" methods.

public function __construct(UserRepository $users)
{
    $this->users = $users;
}

In this example, the UserController needs to retrieve users from a data source. So, we will inject a service that is able to retrieve users. In this context, our UserRepository most likely uses Eloquent to retrieve user information from the database. However, since the repository is injected, we are able to easily swap it out with another implementation. We are also able to easily "mock", or create a dummy implementation of the UserRepository when testing our application.

https://laravel.com/docs/5.3/container

Alexey Mezenin
  • 158,981
  • 26
  • 290
  • 279
  • thx Alexey Mezenin. Can you explain me a little bit if i wanna do this for user login : UserLoginController -> LoginService -> LoginReposity . – Manas Jan 25 '17 at 08:41
  • @MohamedManas I'd recommend you to use [Laravel built-in auth scaffold](https://laravel.com/docs/5.3/authentication#introduction) You don't need to create a repository or a service for that. – Alexey Mezenin Jan 25 '17 at 08:51
  • yes your right, with the built in Auth, the whole process becomes a lot easier, i made it that way in the beginning.. but they want me to go through this stucture :( .. UserLoginController -> LoginService -> Loginrepository -> DB . thats why im stuck – Manas Jan 25 '17 at 09:08
  • @MohamedManas tell them they are reinventing the wheel and using bad practices. I guess you need to find some repository and IoC related tutorials and read/watch them. Also, you could look at standard Laravel's `LoginController.php` and `PasswordController.php` to understand how it works and create something similar. – Alexey Mezenin Jan 25 '17 at 09:16
  • i tried telling them, they wanna keep this structure..><.. thats a good idea to look in to the standard laravel's controller...thank you. if i make it will come back and let u know. – Manas Jan 25 '17 at 09:31
0

This is a design pattern, it's called depedency injection.
This is a good way to work, so you can easyly write tests, or change the services, and more.

You can read more info about dependecy injection here on SO itself, or here on wikipedia.

Blaatpraat
  • 2,829
  • 11
  • 23
  • thx for the info... the link is talking about the repository from the controller... im still confused.. like for example if i have a UserLoginController and i need to do this : UserLoginController->LoginService->LoginRepository->modal. after modal i get it its simply sending data to the database..but how do i go about doing it from UserLoginController till LoginRepository? sorry for the ignorance... – Manas Jan 25 '17 at 08:36