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:
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.