Artisan Console
[[toc]]
Introduction
Artisan is the command line interface included with Goravel, the module can be operated using facades.Artisan()
. It provide a number of helpful commands that can assist you while you build your application. You can use the command blow to get all commands:
Every command also includes a "help" which displays and describes the command's available arguments and options. To view a help screen, precede the name of the command with help:
Instead of repeating go run . artisan ...
command, you may want to add an alias to your shell configuration with the terminal command below:
Then you could simply run your commands like this:
Generating Commands
You can use the make:command
command to create a new command in the app/console/commands
directory. Don't worry if this directory does not exist in your application, it will be created the first time you run the make:command
command:
Command Structure
After generating your command, you should define appropriate values for the signature and description properties of the struct. The handle
method will be called when your command is executed. You need to optimize your logic in this method.
Defining Input Expectations
When writing console commands, it is common to gather input from the user through arguments or options. Goravel makes it very convenient to get arguments and options which user input.
Arguments
Follow the arguments after the command:
Get arguments:
Options
Options, like arguments, are another form of user input. Options are prefixed by two hyphens (--) when they are provided via the command line.
Definition:
Get:
Usage:
Notice: When using both arguments and options, the options need defined before the arguments. Example:
Except command.StringFlag
, we can also use other type Flag
and Option*
: StringSliceFlag
, BoolFlag
, Float64Flag
, Float64SliceFlag
, IntFlag
, IntSliceFlag
, Int64Flag
, Int64SliceFlag
.
Category
You can set a set of commands to the same category, convenient in go run . artisan list
:
Registering Commands
All of your console commands needs to be registered within the Commands
function of the app\console\kernel.go
file.
Programmatically Executing Commands
Sometimes you may wish to execute an Artisan command outside of the CLI, you can use the Call
method on the facades.Artisan()
to operation this.
Last updated