Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 3536

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 61k
Author
Asked: November 26, 20242024-11-26T03:38:08+00:00 2024-11-26T03:38:08+00:00

Laravel Service Classes Explained

  • 61k

Benjamin Franklin once said —

A place for everything, everything in its place.

This applies to software development as well. Understanding which portion of the code goes where is the key to a maintainable code base.

Laravel being an elegant web framework, comes with a pretty organized directory structure by default but still I've seen a lot of people suffer.

Don't get me wrong. It's a no brainer that controllers go inside the controllers directory, no confusions whatsoever. The thing people often confuse themselves with is, what to write in a controller and what not to.

Table of Content

  • Project Codes
  • The Scenario
  • Understanding Business Logic
  • Service Classes to the Rescue
  • Action Re-usability
  • Closing Thoughts

Project Codes

You can find an implementation of the service discussed in this article in the following repository:

GitHub logo fhsinchy / laravel-livewire-shopping-cart

A real-time shopping cart powered by Laravel, Livewire and TailwindCSS

Apart from Laravel, the project makes use of Livewire and TailwindCSS.

The Scenario

Take the following piece of code for example:

<?php  namespace AppHttpControllers;  use IlluminateHttpRequest;  class CartItemController extends Controller {     /**      * Display a listing of the resource.      *      * @return IlluminateHttpResponse      */     public function index()     {         $content = session()->has('cart') ? session()->get('cart') : collect([]);         $total = $content->reduce(function ($total, $item) {             return $total += $item->get('price') * $item->get('quantity');         });          return view('cart.index', [             'content' => $content,             'total' => $total,         ]);     }      /**      * Store a newly created resource in storage.      *      * @param  IlluminateHttpRequest  $request      * @return IlluminateHttpResponse      */     public function store(Request $request)     {         $request->validate([             'name' => 'required|string',             'price' => 'required|numeric',             'quantity' => 'required|integer',         ]);          $cartItem = collect([             'name' => $request->name,             'price' => floatval($request->price),             'quantity' => intval($request->quantity),             'options' => $request->options,         ]);          $content = session()->has('cart') ? session()->get('cart') : collect([]);          $id = request('id');          if ($content->has($id)) {             $cartItem->put('quantity', $content->get($id)->get('quantity') + $request->quantity);         }          $content->put($id, $cartItem);          session()->put('content', $content);          return back()->with('success', 'Item added to cart');     }      /**      * Display the specified resource.      *      * @param  int  $id      * @return IlluminateHttpResponse      */     public function show($id)     {         $content = session()->get('cart');          if ($content->has($id)) {             $item = $content->get($id);              return view('cart', compact('item'));         }          return back()->with('fail', 'Item not found');     }      /**      * Update the specified resource in storage.      *      * @param  IlluminateHttpRequest  $request      * @param  int  $id      * @return IlluminateHttpResponse      */     public function update(Request $request, $id)     {         $content = session()->get('cart');          if ($content->has($id)) {             $cartItem = $content->get($id);              switch ($request->action) {                 case 'plus':                     $cartItem->put('quantity', $content->get($id)->get('quantity') + 1);                     break;                 case 'minus':                     $updatedQuantity = $content->get($id)->get('quantity') - 1;                      if ($updatedQuantity < 1) {                         $updatedQuantity = 1;                     }                      $cartItem->put('quantity', $updatedQuantity);                     break;             }              $content->put($id, $cartItem);              session()->put('cart', $content);              return back()->with('success', 'Item updated in cart');         }          return back()->with('fail', 'Item not found');     }      /**      * Remove the specified resource from storage.      *      * @param  int  $id      * @return IlluminateHttpResponse      */     public function destroy($id)     {         $content = session()->get('cart');          if ($content->has($id)) {             session()->put('cart', $content->except($id));              return back()->with('success', 'Item removed from cart');         }          return back()->with('fail', 'Item not found');     } } 
Enter fullscreen mode Exit fullscreen mode

This is a controller from an imaginary e-commerce project, responsible for managing the shopping cart. Although this is a perfectly valid piece of code, there are some problems.

The controller also knows too much. Look at the index method for example. It doesn't need to know whether the cart content comes from the session or database. Neither does it need to know how to calculate the total price of the cart items.

A controller should be only responsible for transporting requests and responses. The inner details aka the business logic should be handled by other classes in the server.

Understanding Business Logic

As explained in this thread:

Business logic or domain logic is that part of the program which encodes the real-world business rules that determine how data can be created, stored, and changed. It prescribes how business objects interact with one another, and enforces the routes and the methods by which business objects are accessed and updated.

In case of a simple shopping cart system, the business logic behind adding an item to cart can be described as follows:

  1. Take necessary product information (id, name, price, quantity) as input.
  2. Validate the input data.
  3. Form a new cart item.
  4. Check if the item already exists in the cart.
  5. If yes, update it's quantity and if no, add the newly formed item to cart.

Now lets have a look at the store method:

    /**      * Store a newly created resource in storage.      *      * @param  IlluminateHttpRequest  $request      * @return IlluminateHttpResponse      */     public function store(Request $request)     {         // validate the input data.         $request->validate([             'name' => 'required|string',             'price' => 'required|numeric',             'quantity' => 'required|integer',         ]);          // form a new cart item.         $cartItem = collect([             'name' => $request->name,             'price' => floatval($request->price),             'quantity' => intval($request->quantity),             'options' => $request->options,         ]);          // check if the item already exists in the cart.         $content = session()->has('cart') ? session()->get('cart') : collect([]);         $id = request('id');         if ($content->has($id)) {             // if yes, update it's quantity             $cartItem->put('quantity', $content->get($id)->get('quantity') + $request->quantity);         }          // if no, add the newly formed item to cart.         $content->put($id, $cartItem);         $this->session->put('content', $content);          return back()->with('success', 'Item added to cart');     } 
Enter fullscreen mode Exit fullscreen mode

As you can see, the business logic translates to code pretty accurately. Now the problem is, controllers are not meant to contain business logic.

Service Classes to the Rescue

According to the very popular alexeymezenin/laravel-best-practices repository:

Business logic should be in service class

The idea of service classes is not something built into the framework or documented in the official docs. As a result, different people refer to them differently. At the end of the day, service classes are plain classes responsible for holding the business logic.

A service class for holding the shopping cart related business logic can be as follows:

<?php  namespace AppServices;  use IlluminateSupportCollection; use IlluminateSessionSessionManager;  class CartService {     const MINIMUM_QUANTITY = 1;     const DEFAULT_INSTANCE = 'shopping-cart';      protected $session;     protected $instance;      /**      * Constructs a new cart object.      *      * @param IlluminateSessionSessionManager $session      */     public function __construct(SessionManager $session)     {         $this->session = $session;     }      /**      * Adds a new item to the cart.      *      * @param string $id      * @param string $name      * @param string $price      * @param string $quantity      * @param array $options      * @return void      */     public function add($id, $name, $price, $quantity, $options = []): void     {         $cartItem = $this->createCartItem($name, $price, $quantity, $options);          $content = $this->getContent();          if ($content->has($id)) {             $cartItem->put('quantity', $content->get($id)->get('quantity') + $quantity);         }          $content->put($id, $cartItem);          $this->session->put(self::DEFAULT_INSTANCE, $content);     }      /**      * Updates the quantity of a cart item.      *      * @param string $id      * @param string $action      * @return void      */     public function update(string $id, string $action): void     {         $content = $this->getContent();          if ($content->has($id)) {             $cartItem = $content->get($id);              switch ($action) {                 case 'plus':                     $cartItem->put('quantity', $content->get($id)->get('quantity') + 1);                     break;                 case 'minus':                     $updatedQuantity = $content->get($id)->get('quantity') - 1;                      if ($updatedQuantity < self::MINIMUM_QUANTITY) {                         $updatedQuantity = self::MINIMUM_QUANTITY;                     }                      $cartItem->put('quantity', $updatedQuantity);                     break;             }              $content->put($id, $cartItem);              $this->session->put(self::DEFAULT_INSTANCE, $content);         }     }      /**      * Removes an item from the cart.      *      * @param string $id      * @return void      */     public function remove(string $id): void     {         $content = $this->getContent();          if ($content->has($id)) {             $this->session->put(self::DEFAULT_INSTANCE, $content->except($id));         }     }      /**      * Clears the cart.      *      * @return void      */     public function clear(): void     {         $this->session->forget(self::DEFAULT_INSTANCE);     }      /**      * Returns the content of the cart.      *      * @return IlluminateSupportCollection      */     public function content(): Collection     {         return is_null($this->session->get(self::DEFAULT_INSTANCE)) ? collect([]) : $this->session->get(self::DEFAULT_INSTANCE);     }      /**      * Returns total price of the items in the cart.      *      * @return string      */     public function total(): string     {         $content = $this->getContent();          $total = $content->reduce(function ($total, $item) {             return $total += $item->get('price') * $item->get('quantity');         });          return number_format($total, 2);     }      /**      * Returns the content of the cart.      *      * @return IlluminateSupportCollection      */     protected function getContent(): Collection     {         return $this->session->has(self::DEFAULT_INSTANCE) ? $this->session->get(self::DEFAULT_INSTANCE) : collect([]);     }      /**      * Creates a new cart item from given inputs.      *      * @param string $name      * @param string $price      * @param string $quantity      * @param array $options      * @return IlluminateSupportCollection      */     protected function createCartItem(string $name, string $price, string $quantity, array $options): Collection     {         $price = floatval($price);         $quantity = intval($quantity);          if ($quantity < self::MINIMUM_QUANTITY) {             $quantity = self::MINIMUM_QUANTITY;         }          return collect([             'name' => $name,             'price' => $price,             'quantity' => $quantity,             'options' => $options,         ]);     } } 
Enter fullscreen mode Exit fullscreen mode

As I've already said, service classes are not something built into the framework hence there is no artisan make command for creating a service class. You keep the classes anywhere you like. I'm keeping my classes inside App/Services directory.

The CartService.php file contains both public and protected methods. The public methods named add(), remove(), update(), clear() are responsible for adding item to cart, removing item from cart, updating cart item quantity and clearing the cart respectively.

The other public methods are content() and total() responsible for returning the cart content and total price of added items respectively.

Finally the protected methods getContent() and createCartItem() are responsible for returning cart content within the class methods and forming a new cart item from the received parameters.

Now that the service class is ready, you need to use it inside the controller. To utilize the service class inside the CartItemController.php file, the code needs to be updated as follows:

<?php  namespace AppHttpControllers;  use IlluminateHttpRequest; use AppServicesCartService; use AppHttpRequestsCartItemRequest;  class CartItemController extends Controller {     protected $cartService;      /**      * Instantiate a new controller instance.      *      * @return void      */     public function __construct(CartService $cartService)     {         $this->cartService = $cartService;     }      /**      * Display a listing of the resource.      *      * @return IlluminateHttpResponse      */     public function index()     {         $content = $this->cartService->content();         $total = $this->cartService->total();          return view('cart.index', [             'content' => $content,             'total' => $total,         ]);     }      /**      * Store a newly created resource in storage.      *      * @param  AppHttpRequestsCartItemRequest  $request      * @return IlluminateHttpResponse      */     public function store(CartItemRequest $request)     {         $this->cartService->add($request->id, $request->name, $request->price, $request->quantity, $request->options);          return back()->with('success', 'Item added to cart');     }      /**      * Display the specified resource.      *      * @param  int  $id      * @return IlluminateHttpResponse      */     public function show($id)     {         $content = $this->cartService->content();          $item = $content->get($id);          return view('cart', compact('item'));     }      /**      * Update the specified resource in storage.      *      * @param  IlluminateHttpRequest  $request      * @param  int  $id      * @return IlluminateHttpResponse      */     public function update(Request $request, $id)     {         $this->cartService->update($id, $request->id);          return back()->with('success', 'Item updated in cart');     }      /**      * Remove the specified resource from storage.      *      * @param  int  $id      * @return IlluminateHttpResponse      */     public function destroy($id)     {         $this->cartService->remove($id);          return back()->with('success', 'Item removed from cart');     } } 
Enter fullscreen mode Exit fullscreen mode

Thanks to zero configuration resolution, the service container resolves any class that doesn't depend on any interface automatically. Hence simply injecting the CartService in the controller constructor does the trick:

class CartItemController extends Controller {     protected $cartService;      /**      * Instantiate a new controller instance.      *      * @return void      */     public function __construct(CartService $cartService)     {         $this->cartService = $cartService;     } } 
Enter fullscreen mode Exit fullscreen mode

Now an instance of the CartService class becomes available within the controller and can be accessed as $this->cartService property. Rest of the controller action have been updated to make use of the service and as you can see, the controller has become much cleaner now.

Action Re-usability

Apart from making the controller cleaner, you also get the benefit of the shopping cart related actions being accessible from anywhere. Consider the following livewire component for example:

<?php  namespace AppHttpLivewire;  use AppFacadesCart; use LivewireComponent; use IlluminateContractsViewView;  class ProductComponent extends Component {     public $product;     public $quantity;      /**      * Mounts the component on the template.      *      * @return void      */     public function mount(): void     {         $this->quantity = 1;     }      /**      * Renders the component on the browser.      *      * @return IlluminateContractsViewView      */     public function render(): View     {         return view('livewire.product');     }      /**      * Adds an item to cart.      *      * @return void      */     public function addToCart(): void     {         Cart::add($this->product->id, $this->product->name, $this->product->unit_price, $this->quantity);         $this->emit('productAddedToCart');     } } 
Enter fullscreen mode Exit fullscreen mode

You can add, remove, update or clear the shopping cart from anywhere. Prior to the service class implementation, the only way to manage the cart was through HTTP requests. Now you can even manage the cart through artisan commands.

Closing Thoughts

The concept of service classes discussed in this article is nothing concrete and I'm not claiming it to be a silver bullet. It's something that I've used in the past and have had no problem whatsoever. As long as you're thoughtful about using these classes and not overdoing it, you should be good to go.

laravelphptutorialwebdev
  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 2k
  • Popular
  • Answers
  • Author

    ES6 - A beginners guide - Template Literals

    • 0 Answers
  • Author

    Understanding Higher Order Functions in JavaScript.

    • 0 Answers
  • Author

    Build a custom video chat app with Daily and Vue.js

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.