What is a controller in laravel?

The controller is the “C” in the “MVC” (Model-View-Controller) architecture that Laravel is based on. A controller can be reduced to this simple definition: it receives a request from the client and returns a response to the client. This is a simple definition as well as the minimum requirements for any given controller. What it does in between the two is usually seen as an “action” by the controller (or “route implementation”). It acts as a second entry point into the application (the first is the request) for the client, which sends the request payload (which we will receive next) to the application, waiting for some type of response (in the form of a success page, redirect, error page, or any other kind of HTTP response) ).

The controller does (basically) the same as defining a route, with an anonymous function set to “action” when that route is reached. The difference is that the controller does a good job of separating concerns, while the route is determined according to the actual URL definition, which basically means we associate the assigned route URI with the route implementation, or the code that gets executed when that route is hit …

For example, the following two code snippets will achieve the same:

Example # 1: defining and implementing a route within one method call (in the web.php routes file)

// inside routes / web.php
<? php
Route :: get ('/ hello-world', function (Request $ request) {
   $ name = $ request-> name;
   return response () -> make ("<h1> Hello World! This is". $ name, 200);
});

Example # 2. The route definition is in route / web.php, but its implementation is in the class / app / Http / Controllers / HelloWorldController

// inside routes / web.php
<? php

Route :: get ('/ hello-world', 'HelloWorldController @ index') -> name ('hello-world');

-------------------------------------------------- ----------------------------------
// inside app / Http / Controllers / HelloWorldController.php
<? php
namespace App \ Http \ Controllers;
use Illuminate \ Http \ Request;

class HelloWorldController extends Controller
{
   public function index (Request $ request)
   {
       $ name = $ request-> name;
       return response () -> make ("<h1> Hello World! This is". $ name, 200);
   }
}

While example # 2 looks much more time consuming (and it isn’t – just a little more code – that’s it), let’s take a look at the benefits we get by instead placing our action logic for a given “hello-world” route inside a controller from the route definition into as a callback function:

  1. Our logic is clearly divided into a class of its own (separation of interests)
  2. Our controller will be configured to extend later if we need to add more functionality to it … Let’s say maybe we want to add a goodbye function … In this case, we’ll rename the controller to the more generic “HelloController” then define two separate methods, hello () and goodbye () . We would also need to define two separate routes that map the / hello and / goodbye URIs to their respective methods on the controller. This is desirable compared to populating a routes file with the implementation of each route defined as a callback function.
  3. Laravel has a built-in ability to cache all route definitions in an application to speed up the time it takes to find a given route (increases application performance); however, you will only be able to take advantage of this if all the routes you define within the application are configured using controller-specific bindings (see example # 2 above)

Let’s run this command, which will generate a new controller for us.

// ... inside the project's root directory:
php artisan make: controller UploadController   

Basically, this command generates a stub for a controller named “UploadController” inside the main controller directory at /app/Http/Controllers/UploadController.php. Feel free to open this file and see. This is very easy because it is only a mocked version of the controller with the correct namespace path and the required classes from which it extends.

Leave a Reply

Your email address will not be published. Required fields are marked *