What are Custom Commands in CodeIgniter 4?

Why Interviewers Ask This

This is a differentiating question used for senior and lead roles. Interviewers want to see if you can explain not just what happens, but why — and what the trade-offs are in different approaches.

Answer

CodeIgniter 4 allows creating custom Spark commands for CLI tasks. Generate a command: php spark make:command SyncUsers. Commands live in app/Commands/. Define group, name, description, and arguments: protected $group = "custom"; protected $name = "users:sync"; protected $description = "Sync users from external API"; protected $arguments = ["source" => "The data source to sync from"]; protected $options = ["--dry-run" => "Preview without saving"]. The run(array $params) method contains logic. Access arguments: $params[0]. Check options: CLI::getOption("dry-run"). Output: CLI::write("Done!", "green"), CLI::error("Failed!"), CLI::table($headers, $data), CLI::progressBar(100). Get input: CLI::prompt("Enter name"), CLI::promptByKey("Choose", $options). Useful for data imports, scheduled cleanup tasks, and maintenance operations.

Pro Tip

Back up your answer with a specific project or situation. Saying 'In my last CodeIgniter project, I used this when...' immediately makes your answer more credible and memorable.