Console Commands

In addition to a web framework, CakePHP also provides a console framework forcreating command line tools & applications. Console applications are ideal forhandling a variety of background & maintenance tasks that leverage your existingapplication configuratino, models, plugins and domain logic.

CakePHP provides several console tools for interacting with CakePHP featureslike i18n and routing that enable you to introspect your application andgenerate related files.

The CakePHP Console

The CakePHP Console uses a dispatcher-type system to load a commands, parsetheir arguments and invoke the correct command. While the examples below usebash the CakePHP console is compatible with any *nix shell and windows.

A CakePHP application contains src/Command, src/Shell andsrc/Shell/Task directories that contain its shells and tasks. It alsocomes with an executable in the bin directory:

  1. $ cd /path/to/app
  2. $ bin/cake

Note

For Windows, the command needs to be bin\cake (note the backslash).

Running the Console with no arguments will list out available commands. Youcould then run the any of the listed commands by using its name:

  1. # run server shell
  2. bin/cake server
  3.  
  4. # run migrations shell
  5. bin/cake migrations -h
  6.  
  7. # run bake (with plugin prefix)
  8. bin/cake bake.bake -h

Plugin commands can be invoked without a plugin prefix if the commands’s namedoes not overlap with an application or framework shell. In the case that twoplugins provide a command with the same name, the first loaded plugin will getthe short alias. You can always use the plugin.command format tounambiguously reference a command.

Console Applications

By default CakePHP will automatically discover all the commands in yourapplication and its plugins. You may want to reduce the number of exposedcommands, when building standalone console applications. You can use yourApplication’s console() hook to limit which commands are exposed andrename commands that are exposed:

  1. // in src/Application.php
  2. namespace App;
  3.  
  4. use App\Shell\UserShell;
  5. use App\Shell\VersionShell;
  6. use Cake\Http\BaseApplication;
  7.  
  8. class Application extends BaseApplication
  9. {
  10. public function console($commands)
  11. {
  12. // Add by classname
  13. $commands->add('user', UserCommand::class);
  14.  
  15. // Add instance
  16. $commands->add('version', new VersionCommand());
  17.  
  18. return $commands;
  19. }
  20. }

In the above example, the only commands available would be help, versionand user. See the Commands section for how to add commands inyour plugins.

Note

When adding multiple commands that use the same Command class, the helpcommand will display the shortest option.

Renaming Commands

There are cases where you will want to rename commands, to create nestedcommands or subcommands. While the default auto-discovery of commands will notdo this, you can register your commands to create any desired naming.

You can customize the command names by defining each command in your plugin:

  1. public function console($commands)
  2. {
  3. // Add commands with nested naming
  4. $commands->add('user dump', UserDumpCommand::class)
  5. $commands->add('user:show', UserShowCommand::class)
  6.  
  7. // Rename a command entirely
  8. $commands->add('lazer', UserDeleteCommand::class)
  9.  
  10. return $commands;
  11. }

When overriding the console() hook in your application, remember tocall $commands->autoDiscover() to add commands from CakePHP, yourapplication, and plugins.

If you need to rename/remove any attached commands, you can use theConsole.buildCommands event on your application event manager to modify theavailable commands.

Commands

See the Command Objects chapter on how to create your firstcommand. Then learn more about commands:

CakePHP Provided Commands

Routing in the Console Environment

In command-line interface (CLI), specifically your shells and tasks,env('HTTP_HOST') and other webbrowser specific environment variables are notset.

If you generate reports or send emails that make use of Router::url() thosewill contain the default host http://localhost/ and thus resulting ininvalid URLs. In this case you need to specify the domain manually.You can do that using the Configure value App.fullBaseUrl from yourbootstrap or config, for example.

For sending emails, you should provide Email class with the host you want tosend the email with:

  1. use Cake\Mailer\Email;
  2.  
  3. $email = new Email();
  4. $email->setDomain('www.example.org');

This asserts that the generated message IDs are valid and fit to the domain theemails are sent from.