What are Custom Commands in CodeIgniter 4?
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.
Previous
How does CI4 compare to Laravel for large-scale applications?
Next
What is CI4's approach to API versioning?