Menu

CLI Command Processing

Relevant source files

This document explains how the Laravel application processes command-line interface (CLI) commands through the Artisan console system. It covers the command execution lifecycle from the artisan script entry point through command registration, routing, and execution.

For information about HTTP request processing, see HTTP Request Processing. For details about the application bootstrap process that underlies both HTTP and CLI processing, see Application Bootstrap and Lifecycle.

CLI Entry Point and Bootstrap Process

The Laravel application provides CLI access through the artisan script located in the project root. This script serves as the entry point for all console commands and follows a bootstrap process similar to HTTP requests but optimized for command-line execution.

Artisan Script Structure

The artisan script begins with a PHP shebang and establishes the execution environment:

The script performs these key steps:

  1. Sets the LARAVEL_START constant for performance timing artisan7
  2. Loads the Composer autoloader for dependency resolution artisan10
  3. Bootstraps the Illuminate\Foundation\Application instance artisan14
  4. Creates a Symfony\Component\Console\Input\ArgvInput object to parse command-line arguments artisan16
  5. Delegates to $app->handleCommand() for command processing artisan16
  6. Exits with the returned status code artisan18

Sources: artisan1-18

Application Bootstrap for CLI Commands

The bootstrap process loads the same bootstrap/app.php configuration used for HTTP requests. The artisan script and public/index.php both follow identical bootstrap patterns but handle different types of input:

Entry PointInput TypeHandler Method
artisanSymfony\Component\Console\Input\ArgvInput$app->handleCommand()
public/index.phpIlluminate\Http\Request$app->handleRequest()

Shared Bootstrap Configuration Bridge

The Application::configure() method in bootstrap/app.php7 establishes console command routing through the commands parameter bootstrap/app.php10 which points to routes/console.php for command registration.

Sources: artisan14-16 public/index.php18-20 bootstrap/app.php7-18

Command Registration and Routing

Laravel provides multiple mechanisms for registering console commands, with the most common approach being through the routes/console.php file for simple commands and dedicated command classes for complex functionality.

Console Route Registration

Laravel provides command registration through the console routing system configured in bootstrap/app.php. Commands can be registered using closure-based patterns or dedicated command classes:

Command Registration Architecture

The registration process supports:

  • Artisan::command() method for closure-based commands
  • Closure functions with access to console output methods like $this->comment()
  • ->purpose() method for command descriptions
  • Command classes extending Illuminate\Console\Command

Sources: bootstrap/app.php10

Command Execution Context

Commands registered through Artisan::command() execute within a console context that provides:

FeatureDescriptionUsage in Example
$this->comment()Output styled text to consoleDisplaying the inspiring quote
Facade AccessFull access to Laravel servicesInspiring::quote() facade method
Argument/Option ParsingAutomatic handling of CLI parametersInherited from Symfony Console

Command Execution Flow

The complete command execution flow demonstrates how CLI input is processed through Laravel's console system:

Command Resolution Process

The Illuminate\Foundation\Application resolves and executes commands through several steps:

  1. Argument Parsing: Symfony\Component\Console\Input\ArgvInput parses command-line arguments artisan5-16
  2. Command Matching: Laravel matches the command signature against commands registered in routes/console.php bootstrap/app.php10
  3. Handler Resolution: The appropriate command handler (closure or class) is resolved from the console routes
  4. Execution Context: A console context is established with access to Laravel services and facades
  5. Output Handling: Command output is formatted using Symfony Console components via methods like $this->comment()
  6. Status Code: The command returns an integer exit status (0 for success, non-zero for errors)

The $app->handleCommand(new ArgvInput) method orchestrates this entire process and returns the status code that the artisan script passes to exit($status).

Sources: artisan16-18 bootstrap/app.php10

Built-in vs Custom Commands

Laravel distinguishes between built-in framework commands and application-specific custom commands:

Built-in vs Custom Commands

Laravel includes numerous built-in commands accessible through the same artisan entry point:

  • php artisan migrate - Database migrations
  • php artisan serve - Development server
  • php artisan test - Test execution
  • php artisan config:cache - Configuration caching

Custom Command Registration

Custom commands can be registered through multiple approaches:

MethodFile LocationUse Case
Artisan::command()routes/console.phpSimple closure-based commands
Command Classesapp/Console/Commands/Complex commands extending Illuminate\Console\Command
Service Providersapp/Providers/Commands requiring dependency injection

All commands, whether built-in or custom, are processed through the same $app->handleCommand() method in the artisan script.

Sources: artisan16

Command Output and Error Handling

Console commands utilize Symfony Console's output system through Laravel's console context:

The command execution ultimately returns to the artisan script, which exits with the appropriate status code to indicate success or failure to the operating system.

Sources: artisan16-18 routes/console.php7

Morty Proxy This is a proxified and sanitized view of the page, visit original site.