laravel-zero/framework
Laravel Zero is an unofficial, console-optimized micro-framework based on Laravel. It provides an elegant starting point for CLI apps, with optional Eloquent and logging, interactive menus, desktop notifications, scheduling, standalone compilation, and Collision error reporting.
Installation:
composer create-project laravel-zero/laravel-zero my-cli-app
Or add to an existing project:
composer require laravel-zero/framework
Create a Command:
php artisan make:command Greet
Edit app/Commands/Greet.php:
protected $signature = 'greet {name?}';
protected $description = 'Greet someone';
public function handle()
{
$name = $this->argument('name') ?: 'World';
$this->info("Hello, {$name}!");
}
Run the Command:
php artisan greet John
Build a self-contained CLI tool (e.g., a deployment helper) with:
$signature and $this->option().laravel/prompts (built-in):
$name = Prompt::text('What is your name?');
$this->info('Deploying...'); // Outputs to console
\Log::info('Deployment started');
Command-Based Development:
app/Commands/ (e.g., DeployCommand, BackupCommand).public function __construct(private DatabaseService $db) {}
artisan helper:
$this->artisan('greet')->expectsOutput('Hello, World!');
Interactive Menus:
Menu facade (from spatie/laravel-menu or custom):
use LaravelZero\Framework\Menus\Menu;
Menu::new()
->title('Main Menu')
->addOption('Deploy', fn() => $this->call('deploy'))
->addOption('Backup', fn() => $this->call('backup'))
->display();
Scheduling:
app/Console/Kernel.php:
protected function schedule(Schedule $schedule)
{
$schedule->command('backup')->daily();
}
php artisan schedule:run
Standalone PHARs:
php artisan build
php artisan build:config (e.g., PHP version, dependencies).Database Integration:
use App\Models\User;
$users = User::all();
php artisan migrate
nunomaduro/collision for rich error pages:
$this->callSilently('deploy'); // Silently fail
.env files for config (e.g., DB_CONNECTION=sqlite::memory for testing).Artisan class to add custom commands globally.PHAR Build Issues:
php artisan build fails with pcntl errors.
Fix: Install pcntl or use --no-pcntl:
php artisan build --no-pcntl
database/migrations is included in build.php:
return [
'mainClass' => 'App\Kernel',
'includeFiles' => ['database/migrations'],
];
Dependency Conflicts:
replace in composer.json or alias classes:
"extra": {
"laravel": {
"dont-discover": ["eloquent"]
}
}
Logging Quirks:
storage/logs.
Fix: Ensure APP_LOG is set in .env:
APP_LOG=single
APP_LOG_LEVEL=debug
Interactive Prompts in Non-TTY:
Prompt::text() fails in cron jobs.
Fix: Check for TTY or use --ansi flag:
if (! $this->output->isDecorated()) {
$this->output->setDecorated(true);
}
$this->call('collision:serve');
--verbose:
php artisan greet --verbose
php -d phar.readonly=0 vendor/bin/your-phar.phar
Custom Command Bus:
app/Providers/CommandServiceProvider.php to bind custom commands:
public function register()
{
$this->commands([
\App\Commands\CustomCommand::class,
]);
}
Menu Extensions:
MenuServiceProvider to register global menus:
public function boot()
{
Menu::macro('admin', fn() => Menu::new()->title('Admin'));
}
Scheduler Events:
$schedule->command('backup')->before(fn() => Log::info('Backup started'));
PHAR Customization:
build.php to include assets or scripts:
return [
'mainClass' => 'App\Kernel',
'includeFiles' => ['config/*', 'routes/*'],
'postBuildCommands' => [
'chmod +x storage/bin/hook.sh',
],
];
config/app.php between web and CLI apps.app/Providers/AppServiceProvider for both web and CLI.artisan helper in tests:
$this->artisan('command:name', ['option' => 'value'])
->expectsQuestion('Confirm?', 'yes')
->expectsOutput('Success!');
Cache::remember:
$data = Cache::remember('command:data', now()->addHours(1), fn() => $this->fetchData());
How can I help you explore Laravel packages today?