How to use Laravel 8 Controller

What is the purpose of Laravel Controller?

During my development experience with Laravel I have seen that developer they do not know the purpose of the controller and they abuse the controller. You need to understand the purpose of the controller so that you can efficiently use it.

You also need to learn on what things are available in the controller so that you can efficiently use the controller without writing messy code. Followings are some good examples of why we need controller:

  • Controller should take Request object and then extract the data it needs
  • Controller should perform validation on incoming Request data
  • Controller should not need to implement all business logic inside rather you can call a service to run your business logic and return data
  • Once your service returns appropriate data you can send this data to your view

Let's look at the diagram:

In some cases you do not have to run business logic in that case pass the needed data for the view to laravel view directly.

In this tutorial I will not deep dive however we would just look at one sample controller to figure out our stratrgy.

<?php

namespace App\Http\Controllers;

use Illuminate\Contracts\Foundation\Application;
use App\Http\Requests\CartItemStoreRequest;
use App\Http\Service\ShoppingCartService;
use Illuminate\Contracts\View\Factory;
use Illuminate\Contracts\View\View;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class ShoppingCartController extends Controller
{
    /**
     * @param Request $request
     * @return Application|Factory|View
     */
    public function list(Request $request)
    {
        return view('cart');
    }

    /**
     * @param CartItemStoreRequest $request
     * @param ShoppingCartService $service
     * @return JsonResponse
     */
    public function store(CartItemStoreRequest $request, ShoppingCartService $service): JsonResponse
    {
        // Retrieve the validated input data...
        $validatedData = $request->validated();

        // your service handle business logic
        $cartItem = $service->addItem($validatedData);

        // passing data to view
        return response()->json([
            'item' => $cartItem->getItem()
        ]);
    }
}

Explaination:

  • If you look at the list method it is very simple we do not need any validation or to run any business logic
  • let's look at our ShoppingCartController@store method in detail
    • In laravel there is a concept of Request validation which is basically a seperate class where you can put your validation logic.
    • Once request is validated you get all the validated data from the request
    • Now, you have created a seperate service which you can inject in your laravel controller using DI concept
    • Now, that the service available you can run your business logic in that service and get data that you need
    • Pass this data to your view and show your data in laravel blade view

What benefits do I get using above strategy?

If you adopt this stratergy you can get following benefits:

  • Your controller looks cleaner
  • Code is dicoupled and easy to unit test
  • Your service is responsible for running business logic