laravel-zero/framework
Laravel Zero is an unofficial, Laravel-based micro-framework for building fast, elegant console apps. Includes optional Eloquent/logging, interactive menus, desktop notifications, scheduler, standalone compiler, and Collision-powered error reporting.
Installation:
composer create-project laravel-zero/laravel-zero my-cli-app
Or add to an existing project:
composer require laravel-zero/framework
First Command:
Create a command in app/Console/Commands/ (e.g., HelloCommand.php):
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class HelloCommand extends Command
{
protected $signature = 'hello {name?}';
protected $description = 'Say hello to someone';
public function handle()
{
$name = $this->argument('name') ?: 'World';
$this->info("Hello, {$name}!");
}
}
Run it:
php artisan hello John
Key Files to Explore:
app/Console/Kernel.php: Command registration and bootstrapping.config/: Framework configuration (logging, Prompts, etc.).artisan: The CLI entry point (customizable via app/Console/Kernel.php).Command Development:
protected $signature = 'migrate:fresh {--database= : Database connection} {--force : Force the operation}';
laravel/prompts):
use Laravel\Prompts\Prompt;
$name = Prompt::text('What is your name?');
$confirm = Prompt::confirm('Proceed?', default: false);
$this->info('Success!');
$this->error('Failed!');
$this->table(['Name', 'Value'], [['Foo', 'Bar']]);
Configuration Management:
config() helper or $this->config in commands.php artisan vendor:publish --tag=laravel-zero-config
.env or config/laravel-zero.php.Task Scheduling:
app/Console/Kernel.php:
protected function schedule(Schedule $schedule)
{
$schedule->command('backup:run')->daily();
$schedule->exec('php artisan optimize')->everyFiveMinutes();
}
php artisan schedule:run
Interactive Menus:
laravel-zero/menus (included) or spatie/laravel-menu:
use LaravelZero\Framework\Menus\Menu;
Menu::new()
->title('Main Menu')
->option('Option 1', fn () => $this->handleOption1())
->option('Exit', fn () => exit)
->display();
PHAR Compilation:
php artisan build
php artisan build:config (e.g., PHP version, dependencies).Logging & Error Handling:
Log::info(), Log::error(), etc.Collision for rich error reporting (auto-integrated):
$this->callSilently('deploy', ['--verbose']);
$this->app->bind('MyService', fn () => new MyService());
composer require illuminate/database and configure .env.
Use DB::table() or Eloquent models in commands.use Tests\TestCase;
class HelloCommandTest extends TestCase
{
public function test_hello_command()
{
$this->artisan('hello', ['name' => 'John'])
->expectsOutput('Hello, John!');
}
}
PHAR Build Issues:
php artisan build fails with pcntl errors.
Fix: Install pcntl or use --no-pcntl flag:
php artisan build --no-pcntl
composer.json under require and use php artisan build:config to include them.Configuration Overrides:
php artisan build --force
Prompt Version Conflicts:
laravel/prompts version mismatch.
Fix: Ensure compatibility (Laravel Zero v10+ supports 0.2–0.3):
composer require laravel/prompts:^0.3
Scheduler Not Running:
schedule:run is called via cron or systemd:
* * * * * cd /path-to-app && php artisan schedule:run >> /dev/null 2>&1
Eloquent in PHAR:
.env is included in the PHAR or use --env flag:
php artisan build --env=production
Enable Debug Mode:
php artisan --env=local --debug
Or set APP_DEBUG=true in .env.
Collision Error Reporting:
php artisan collision:install to enable rich error pages.http://localhost:8000/collision during development.Logging:
storage/logs/laravel.log.php artisan log:clear to reset logs.Command Debugging:
$this->dump($variable);
$this->pause('Press enter to continue...');
Custom Command Macros:
Extend the Command class globally in app/Providers/AppServiceProvider.php:
use Illuminate\Console\Command;
Command::macro('successMessage', function ($message) {
$this->info("[SUCCESS] {$message}");
});
Usage:
$this->successMessage('Operation completed!');
Custom Artisan Commands:
Override the artisan binary path in app/Console/Kernel.php:
protected function configureArtisan()
{
$this->artisan->binaryPath('/custom/path/to/artisan');
}
Custom PHAR Compiler:
Extend the BuildCommand to add pre/post-build steps:
use LaravelZero\Framework\Commands\BuildCommand;
class CustomBuildCommand extends BuildCommand
{
protected function getExtraBoxOptions()
{
return ['--platform' => 'linux'];
}
}
Interactive Menu Extensions:
Create custom menu items or themes by extending LaravelZero\Framework\Menus\Menu.
Event Listeners:
Listen to command events (e.g., CommandStarting, CommandFinished):
use Illuminate\Console\Events\CommandStarting;
public function boot()
{
CommandStarting::listen(function (CommandStarting $event) {
Log::debug("Command starting: {$event->commandName}");
});
}
Environment Variables:
.env by default. Use --env flag or include .env in the PHAR:
php artisan build --env=production --include-env
Logging Channels:
logging in config/laravel-zero.php to use custom channels (e.g., Slack, Syslog).Prompt Defaults:
config/prompts.php:
'defaults' => [
'confirm' => ['default' => false],
'text' => ['default' => ''],
],
Scheduler Timezones:
APP_TIMEZONE in .env to avoid timezone issues in scheduled tasks.PHAR Optimization:
php artisan build --exclude=tests,node_modules
--optimize to minify included files.How can I help you explore Laravel packages today?