Installation Add the package via Composer:
composer require tomatophp/console-helpers
No additional configuration is required—it’s a drop-in helper library.
First Use Case: Basic Console Output
Import the helper in your Artisan command or service:
use TomatoPHP\ConsoleHelpers\Console;
Use built-in methods like Console::title(), Console::section(), or Console::line():
Console::title('My Command');
Console::line('This is a basic line of output.');
Where to Look First
php artisan vendor:publish --provider="TomatoPHP\ConsoleHelpers\ConsoleServiceProvider"
to publish the package’s config (if applicable).TomatoPHP\ConsoleHelpers\Console class for available methods (e.g., success(), error(), table(), progressBar()).Structured Command Output
Use Console::section(), Console::title(), and Console::line() to organize output hierarchically:
Console::title('Database Migration');
Console::section('Step 1: Backup');
Console::line('Backing up tables...');
Progress Tracking
Leverage progressBar() for long-running tasks:
$progress = Console::progressBar(100);
for ($i = 0; $i < 100; $i++) {
// Simulate work
$progress->advance();
}
$progress->finish();
Styling and Feedback Use color-coded methods for user feedback:
Console::success('Migration completed!');
Console::warning('Some tables were skipped.');
Console::error('Failed to connect to database.');
Tables for Data Display
Format tabular data with table():
$headers = ['ID', 'Name', 'Status'];
$rows = [[1, 'User 1', 'Active'], [2, 'User 2', 'Inactive']];
Console::table($headers, $rows);
Interactive Prompts
Use ask(), confirm(), or choice() for CLI interactions:
$name = Console::ask('Enter your name');
if (Console::confirm('Proceed?')) {
Console::choice('Select an option', ['Option 1', 'Option 2']);
}
Logging and Debugging
Log messages with log() or dump variables with dump():
Console::log('Debug info', ['key' => 'value']);
Console::dump($variable);
Handle class to include Console helpers:
use TomatoPHP\ConsoleHelpers\Console;
use Illuminate\Console\Command;
class MyCommand extends Command {
protected function handle() {
Console::title('Custom Command');
// ...
}
}
Console helpers to a service if needed:
$this->app->singleton('console', function () {
return new \TomatoPHP\ConsoleHelpers\Console();
});
Console class to add domain-specific methods.Method Overrides
Avoid naming conflicts with Laravel’s built-in methods (e.g., line() vs. newLine()). Prefer explicit helpers like Console::line() over direct Symfony\Component\Console\Output calls.
Progress Bar Quirks
$progress->finish()) to avoid hanging output.sleep() or chunk processing to prevent UI lag:
for ($i = 0; $i < 1000; $i++) {
if ($i % 10 === 0) $progress->advance();
}
Interactive Prompts in Non-TTY
Prompts like ask() or confirm() may fail in non-interactive environments (e.g., CI/CD). Use fallbacks:
if (Console::isInteractive()) {
$input = Console::ask('Question?');
} else {
$input = 'default_value';
}
Table Formatting
wordwrap() or limit column width:
$rows = array_map(function ($row) {
$row[1] = wordwrap($row[1], 20, "\n", true);
return $row;
}, $rows);
Deprecation Risks The package is lightweight but lacks active maintenance. Test critical features across Laravel versions (e.g., 8/9/10) for compatibility.
Console::verbose() to log detailed steps:
Console::verbose('Verbose details', ['data' => 'value']);
Console::SUCCESS() vs. Console::success()).Console::output()->flush();
Custom Helpers
Extend the Console class to add domain-specific methods:
class CustomConsole extends \TomatoPHP\ConsoleHelpers\Console {
public function customMethod() {
$this->line('Custom output');
}
}
Bind it in a service provider:
$this->app->singleton('console', function () {
return new CustomConsole();
});
Override Templates
Modify the Console class to change default styles (e.g., colors, formats) by overriding protected methods like getStyle().
Add New Prompt Types
Extend the Prompt class to support custom input validation or formatting.
Integrate with Laravel Logging
Pipe Console::log() output to Laravel’s log channels by binding a custom handler:
Console::setLogger($this->app->make('log'));
How can I help you explore Laravel packages today?