DALT.PHP

Request Lifecycle

Understanding how HTTP requests flow through the framework

Lesson 1: Request Lifecycle

Understanding the HTTP request lifecycle is fundamental to debugging backend issues.

Overview

Every HTTP request follows a predictable path through the framework:

Browser Request

Front Controller (index.php)

Bootstrap (Load config, connect DB)

Router (Match URL to controller)

Middleware (Auth, CSRF, etc.)

Controller (Business logic)

View (Render HTML)

HTTP Response

The Journey of a Request

Let's trace what happens when you visit http://localhost:8000/posts/123

Entry Point

Every request enters through public/index.php:

// public/index.php
define('BASE_PATH', dirname(__DIR__));

require BASE_PATH . '/vendor/autoload.php';

session_start();

require BASE_PATH . '/framework/Core/functions.php';
require BASE_PATH . '/framework/Core/bootstrap.php';

What happens:

  • Define base path constant
  • Load Composer autoloader
  • Start PHP session
  • Load helper functions
  • Bootstrap the framework

Bootstrap

The bootstrap process initializes the framework:

// framework/Core/bootstrap.php
$dotenv = Dotenv\Dotenv::createImmutable(BASE_PATH);
$dotenv->load();

$config = require base_path('config/app.php');
$dbConfig = require base_path('config/database.php');

$container = new Container();
$container->bind(Database::class, function() use ($dbConfig) {
    return new Database($dbConfig);
});

App::setContainer($container);

What happens:

  • Load environment variables from .env
  • Load configuration files
  • Create dependency injection container
  • Connect to database
  • Make container globally available

Routing

The router matches the URL to a controller:

$router = new Router();
require base_path('routes/routes.php');

$uri = parse_url($_SERVER['REQUEST_URI'])['path'];
$method = $_POST['_method'] ?? $_SERVER['REQUEST_METHOD'];

$router->route($uri, $method);

What happens:

  • Create router instance
  • Load route definitions
  • Extract URI from request
  • Determine HTTP method
  • Match route and execute controller

Middleware

Middleware filters the request before reaching the controller:

// In routes/routes.php
$router->get('/dashboard', 'dashboard.php')->only('auth');

What happens:

  • Router checks for middleware
  • Each middleware's handle() method runs
  • If middleware fails, request stops (redirect/abort)
  • If all pass, controller executes

Controller

The controller handles business logic:

// Http/controllers/posts/show.php
$db = App::resolve(Database::class);

$post = $db->query('SELECT * FROM posts WHERE id = :id', [
    'id' => $_GET['id']
])->findOrFail();

view('posts/show.view.php', [
    'post' => $post
]);

What happens:

  • Resolve database from container
  • Query for the post
  • Render view with data

View Rendering

The view generates HTML:

// resources/views/posts/show.view.php
<?php view('partials/head', ['title' => $post['title']]); ?>

<h1><?= htmlspecialchars($post['title']) ?></h1>
<p><?= htmlspecialchars($post['body']) ?></p>

<?php view('partials/foot'); ?>

What happens:

  • Extract variables from array
  • Execute PHP template
  • Output HTML to browser

Key Concepts

Front Controller Pattern

All requests go through a single entry point (index.php). This allows:

  • Centralized configuration
  • Consistent request handling
  • Easy middleware application

Dependency Injection

Services are registered in a container and resolved when needed:

// Register
$container->bind(Database::class, function() {
    return new Database($config);
});

// Resolve
$db = App::resolve(Database::class);

Route Parameters

URLs can contain dynamic segments:

$router->get('/posts/{id}', 'posts/show.php');
// Matches: /posts/123
// Result: $_GET['id'] = 123

Debugging the Lifecycle

Use dd() to Inspect

// In any file
dd($_SERVER); // Dump server variables
dd($router->routes); // Dump registered routes
dd($_SESSION); // Dump session data

Check Each Stage

When debugging, trace the request step by step:

  1. Entry: Is index.php being executed?
  2. Bootstrap: Are configs loaded? Is DB connected?
  3. Routing: Does the route exist? Is it matching?
  4. Middleware: Is middleware blocking the request?
  5. Controller: Is the controller file found?
  6. View: Is the view rendering correctly?

Common Issues

404 Not Found

Symptom: Page not found error

Possible causes:

  • Route not registered in routes/routes.php
  • Wrong HTTP method (GET vs POST)
  • Route order issue (generic before specific)

500 Internal Server Error

Symptom: White screen or error page

Possible causes:

  • PHP syntax error
  • Database connection failed
  • Missing file or class

Blank Page

Symptom: Nothing displays

Possible causes:

  • Output buffering issue
  • Error display disabled
  • View file not found

Practice Exercise

Try tracing a request manually:

  1. Start the server: php artisan serve
  2. Visit: http://localhost:8000/
  3. Add dd('Checkpoint 1') in public/index.php
  4. Refresh the page - you should see the dump
  5. Move the dd() to different files to trace the flow

Next Steps

Now that you understand the request lifecycle, you're ready to learn about routing!

On this page