MVC Lesson 9: Laravel Workflow Using the Analogy

In previous post, we have created a simple analogy (Hotel Room Services) to understand how custom MVC works. In same scenario let’s check how Laravel works.

💡 Laravel Workflow Using the Analogy

1. index.php – The Butler

In Laravel, this file is still the index.php, located in the public folder, and it has a similar role as your custom framework:

require __DIR__.'/../vendor/autoload.php';
$app = require_once __DIR__.'/../bootstrap/app.php';

$kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);
$response = $kernel->handle(
    $request = Illuminate\Http\Request::capture()
);

$response->send();
$kernel->terminate($request, $response);
  • The Butler (index.php):
    • It loads the Laravel bootstrap file, which sets up the application.
    • Then, it asks Laravel to handle the incoming HTTP request.

2. bootstrap/app.php – The Receptionist

In Laravel, bootstrap/app.php acts as the receptionist. It prepares the application and sets up key components, including the routing system, middleware, and service providers.

Here’s the relevant part of bootstrap/app.php:

$app = new Illuminate\Foundation\Application(
    $_ENV['APP_BASE_PATH'] ?? dirname(__DIR__)
);

$kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);

return $app;
  • Receptionist (bootstrap/app.php):
    • Loads the application configuration and service providers.
    • The receptionist knows how to create the Kernel (the application core), which is responsible for managing the request and response lifecycle.

3. Kernel.php – The Room Service Agent

In Laravel, the Kernel.php file, located in the app/Http/Kernel.php directory, is responsible for handling incoming requests. It is similar to your Room Service Agent (App.php), but Laravel handles this in a more automated way.

protected $middleware = [
    \App\Http\Middleware\TrustProxies::class,
    \Illuminate\Http\Middleware\HandleCors::class,
    \Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
    \Illuminate\Session\Middleware\StartSession::class,
    \Illuminate\View\Middleware\ShareErrorsFromSession::class,
    \Illuminate\Routing\Middleware\SubstituteBindings::class,
];
  • Room Service Agent (Kernel.php):
    • It defines middleware that needs to be run for every request (e.g., validating sessions, handling CORS).
    • It also knows how to send the request to the router and let it figure out the right controller and method.

4. Router – The Manager

Laravel’s Router (defined in routes/web.php for web routes) is similar to your Manager (Router.php). This is where you define all your routes and the controller methods that should be invoked when those routes are accessed.

For example:

use App\Http\Controllers\HomeController;

Route::get('/', [HomeController::class, 'home']);
  • Manager (Router):
    • The manager (Router) checks the incoming request and matches the URL (/) and HTTP method (GET) with the defined routes.
    • If a match is found, the manager will instantiate the right controller and call the correct method (e.g., HomeController@home).

5. Controller – The Room

Once the manager (Router) has found the correct route, it creates an instance of the appropriate controller (like the room).

Here’s how the HomeController looks:

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class HomeController extends Controller
{
    public function home()
    {
        return view('home');
    }
}
  • Room (Controller):
    • HomeController handles the request and calls the method (home()) that generates the response.
    • The controller method usually returns a view, which is the actual room the user gets to visit.

🌟 Summary of Laravel vs. Your Custom Framework

Custom FrameworkLaravel
index.php = Butlerindex.php = Butler
bootstrap.php = Receptionistbootstrap/app.php = Receptionist
App.php = Room Service AgentKernel.php = Room Service Agent
Router.php = ManagerRouter (defined in web.php)
Controller = RoomController = Room

🧠 Laravel in Action (Using the Analogy):

  1. User Request: User navigates to / (Home page).
  2. Butler (index.php): The butler (index.php) receives the request and forwards it to the receptionist (bootstrap.php).
  3. Receptionist (bootstrap/app.php): The receptionist sets up the app and hands the request to the room service agent (Kernel.php).
  4. Room Service Agent (Kernel.php): The agent passes the request to the manager (Router) to check which room (controller) is available for this request.
  5. Manager (Router): The manager checks if the requested route / is defined. It finds the route in web.php and checks the associated HomeController and home() method.
  6. Room (Controller): The HomeController‘s home() method is called, and it returns the home page view to the user.

🌱 Key Differences in Laravel:

  1. Automatic Handling of Many Tasks:
    • In your custom framework, you manually set up routing and dispatching. In Laravel, a lot of this is handled automatically with powerful routing and middleware systems.
  2. Built-in Features:
    • Laravel provides many features out-of-the-box, like request handling, middleware (for tasks like authentication, logging, etc.), and session management.
  3. Routing & Middleware:
    • Laravel’s routes are very flexible, and you can also add middleware (before and after the request) for common tasks like authentication.

🚀 Conclusion:

In Laravel, the structure and flow of handling requests are largely the same as your custom framework, but Laravel provides more automation and built-in functionality to make things easier and faster.

If you’d like me to dive deeper into any of these Laravel components or show specific examples of routing, controllers, or middleware, feel free to ask!