Laravel is an elegant web framework , and it already provides the functionality to write clean code. It gives a well-maintained directory structure, but what about the responsibility of the controller?
The controller should be only responsible for transferring the request to the server and returning the response to the client machine. The inner details i.e. business logic should be handled by the other classes on the server. Here, the service class comes into action.
Understanding of Business logic
Business logic, also known as domain logic, is the section of the program that describes the real-world business rules that govern how data is manipulated (for example, creating, storing, updating, etc.).
It defines how business objects interact and enforces the routes and methods by which objects will access and update.
What is Service Class
The service class is a normal PHP class. Laravel does not provide any artisan command for creating service classes like php artisan: The service class is not something built into the laravel framework or described in its official documentation.
When a new user registers into the system. The following steps will be performed:
- Take necessary data from the user. (i.e., name, email, contact no, password)
- Validate the input data.
- Upload a profile photo of the user.
- Create new users.
- Return the response to the user.
Now look at the store() method of the UserController.
As you can see, all the code is pretty clean and well-maintained. The issue is that Controllers are not intended to contain true business logic.
Service Class Comes into Action
The service class is a normal PHP class. The service class is only responsible for holding the actual business logic.
We have to create these classes by ourselves, and laravel allows putting these classes into any directory. Mostly everyone put these classes into the “App\Service” Directory.
As I said, you can keep these service classes anywhere you like. I Keep these classes in the “App\Service” directory.
The UserService.php file contains the create() and uploadProfileImage() protected function. create() method is responsible for creating a new user and adding to the database and returning the instance of the model. uploadProfileImage() method is responsible for uploading a new profile image and returning the path of the uploaded file. Now, we can use this service class in our controller. We have to update your controller as below.
The laravel service container instantly resolves any class that does not rely on any interface thanks to zero configuration resolution. We can simply inject our service class into the controller’s constructor.
Now the instance of the service class is available within the whole controller and we can access this service using the “$this->user” property. Now the controller code is too clean and maintained. The controller is now a lot cleaner.
Benefits of Service Class
- Code reusability : we can use all the methods of the service classes in different artisan commands and different controllers.
- Single-responsibility principle (SRP): We can achieve the SRP by using the Service classes. SRP means, everything has only a single responsibility. For example, if the Controller is used for only transporting the request from the client to the server, it should not contain any kind of business logic. like functions, it has its behavior, addition method only does the addition of numbers.
- Fewer chances of conflict: Using service classes, we can easily manage the code for the larger application when a large development team works on the same project.
There is very little chance of conflict in this structure. Due to the separation of the large code into the small different classes.
Short Code in Controller: Service class will reduce the length of the code in the controller. All the code will be divided into functions, we need to call only methods of the service classes. We know that if something brings an advantage, it always has some drawbacks too.
Drawbacks of Service Class
Too many class files: When we use the service, we need to create too many classes for all the relevant controllers or modules.
Small Applications: Experienced developers are not recommended to use the service classes in small applications. No doubt we can use it in small applications too.
Hire Our Worthy Web Developers
Explore the Benefits of Our Web Get Expert Assistance
As said earlier, every developer has their point of view. There are many similar methods or techniques available to service classes (i.e., Action class, Traits, etc.). Laravel provides the flexibility to choose the structure for your application. This service class is a part of this structure. Stay tuned for more. Happy coding.