Creating Command Controllers¶
Command Namespaces were designed to create a simple command tree with multiple Command Controllers under a common entry point name. It also enables autoloading of commands, by following a known directory/filename structure so you don't need to manually register commands when bootstrapping the application.
Directory Structure¶
Command Namespaces rely on a known directory / filename structure such as the following:
Text Only | |
---|---|
Take the example of Dolphin. Consider the following directory/file structure:
This yields the following command tree:
Text Only | |
---|---|
Creating a Command Controller¶
Let's say you want to create a command named hello
. You should start by creating a new directory under the app/Commands
folder:
Bash | |
---|---|
Now Hello
is your Command Namespace. Inside that directory, you'll need to create at least one Command Controller. You can start with the DefaultController
, which will be called by default when no subcommand is provided.
This is how this DefaultController
class could look like:
PHP | |
---|---|
This command would be available as:
Bash | |
---|---|
Becase a subcommand was not provided, it is inferred that you want to execute the default command. This command can also be invoked as:
Bash | |
---|---|
Any other Command Controller placed inside the Hello
namespace will be available in a similar way. For instance, let's say you want to create a new subcommand like hello caps
.
You would then create a new Command Controller named CapsController
:
PHP | |
---|---|
And this new command would be available as:
Bash | |
---|---|