Skip to main content
Version: CANARY 🚧

BowPHP Console

Introduction​

The BowPHP console is a powerful tool that helps you speed up your development. It provides a set of handy commands to generate classes, test your application locally, manage your databases, and much more.

Usage

All commands are run from the root of your application using the syntax: php bow <command>

Generation commands​

These commands help you quickly create the building blocks of your application:

CommandDescription
generate:resourceCreates a complete REST controller. Example: php bow generate:resource UserController
generate:keyGenerates a new security key for the application
generate:session-tableCreates a migration for the sessions table
generate:notification-tableCreates a migration for the notifications table
generate:cache-tableCreates a migration for the cache table
generate:queue-tableCreates a migration for the queues table
Alias

gen is the alias for generate (and not for add). The examples above can therefore be written as:

php bow gen:resource UserController
php bow gen:key

Adding components​

These commands let you quickly add components to your application:

Controllers and Middleware​

CommandDescription
add:controllerCreates a new controller. Example: php bow add:controller OrderController
add:middlewareCreates a new middleware. Example: php bow add:middleware AuthMiddleware

Models and Database​

CommandDescription
add:modelCreates a new model. Example: php bow add:model Product
add:migrationCreates a new migration. Example: php bow add:migration create_orders_table
add:seederCreates a seeding file. Example: php bow add:seeder UsersSeeder
add:validationCreates a new validator. Example: php bow add:validation UserValidation

Services and Configuration​

CommandDescription
add:serviceCreates a new service. Example: php bow add:service PaymentService
add:configurationCreates a configuration file. Example: php bow add:configuration payment
add:exceptionCreates a new exception. Example: php bow add:exception InvalidUserException

Events and Notifications​

CommandDescription
add:eventCreates a new event. Example: php bow add:event UserRegisteredEvent
add:listenerCreates an event listener. Example: php bow add:listener UserRegisteredListener
add:notifierCreates a new notifier. Example: php bow add:notifier EmailNotifier

Queue and Tasks​

CommandDescription
add:taskCreates a new queued task. Example: php bow add:task SendInvoiceTask
add:schedulerCreates a scheduled task (scheduler). Example: php bow add:scheduler CleanupScheduler
add:commandCreates a custom console command. Example: php bow add:command ImportDataCommand

Migrations​

Migrations make it easy to manage the evolution of your databases:

CommandDescription
migrateApplies all pending migrations
migration:migrateAlias for migrate
migration:rollbackRolls back the last migration
migration:resetResets all migrations
Caution

The migration:reset command deletes all data. Use it carefully in production.

Seeding​

Seeding lets you initialize your database with data:

CommandDescription
seed:allRuns all seeders
seed:file [class]Runs a specific seeder. Example: php bow seed:file UsersSeeder

Queue Worker​

Manage your queued jobs:

CommandDescription
run:workerStarts the worker to process jobs
flush:workerEmpties the queue (use with caution)

Available options for run:worker:

  • --connection: Connection to use (beanstalkd, sqs, redis, database)
  • --queue: Queue name (default: default)
  • --tries: Maximum number of attempts per job
  • --timeout: Timeout in seconds per job
  • --sleep: Pause between jobs in seconds
php bow run:worker --queue=emails --tries=5

Scheduler​

Manage scheduled tasks. The verb is schedule (singular), and there are five actions:

CommandDescription
schedule:runRuns the due tasks once (ideal for a cron running every minute)
schedule:workRuns the scheduler in daemon mode (continuous loop)
schedule:listLists all registered tasks
schedule:nextDisplays the next run time for each task
schedule:test [class]Runs a specific task for testing
# Run once (for cron)
php bow schedule:run

# Daemon mode (runs continuously in a loop)
php bow schedule:work

# View
php bow schedule:list
php bow schedule:next

# Test a task by class name
php bow schedule:test App\\Tasks\\CleanupTask

Clearing the cache​

These commands let you reset the caches:

CommandDescription
clear:viewDeletes the compiled views cache
clear:cacheDeletes the general cache
clear:sessionDeletes the sessions cache
clear:logDeletes the log files
clear:allDeletes all caches

Development server​

CommandDescription
run:serverStarts a local web server
run:consoleStarts an interactive console (REPL)
# Server on the default port (8080)
php bow run:server

# Server on a custom port
php bow run:server --port=3000

# Interactive console
php bow run:console

Creating a custom command​

1. Generate the class​

php bow add:command SendReportCommand

This command creates a file at app/Commands/SendReportCommand.php:

<?php

namespace App\Commands;

use Bow\Console\AbstractCommand;
use Bow\Console\Color;

class SendReportCommand extends AbstractCommand
{
/**
* Method called when the command is invoked. The first argument is
* the raw name of the command as typed by the user.
*/
public function process(): void
{
echo Color::green("Rapport envoyé avec succès !\n");
}
}
Called method

For a class registered as a command, Bow calls the process() method. You can also register a closure (see below) β€” in that case it receives (Argument $arg, Setting $setting).

2. Register the command​

Registration is done via Console::register() (static) or $console->addCommand() (instance). Both methods accept up to four arguments:

use Bow\Console\Console;
use App\Commands\SendReportCommand;

Console::register(
'send:report', // Name used on the command line
SendReportCommand::class, // Class or closure
'Envoie le rapport quotidien', // Description (shown in `php bow help`)
"\nUsage:\n php bow send:report [--to=...]\n" // Help shown by `php bow send:report help`
);

You can place these calls in a dedicated bootstrap file (declared in Setting::getBootstrap()), or in the run() method of a service provider (Bow\Configuration\Configuration).

3. Run​

# Run the command
php bow send:report

# View its help
php bow send:report help

# The command also appears in the "CUSTOM" section of the global help
php bow help
Closures

For simple commands you can register a closure directly:

Console::register('hello', function ($arg, $setting) {
echo "Hello, " . ($arg->getTarget() ?? 'world') . "\n";
});

Is something missing?

If you run into problems with the documentation or have suggestions to improve the documentation or the project in general, please open an issue for us, or send a tweet mentioning the Twitter account @bowframework or directly on github.