Light diffraction prism. In addition to the commands provided with Artisan, you may also build your own custom commands for working with your application. You may store your custom commands in the
app/Console/Commands directory; however, you are free to choose your own storage location as long as your commands can be autoloaded based on your
Service providers are very important when it comes to package development with Laravel. They usually serve as the entry point between your package and the Laravel application making use of your package. They are also responsible for binding classes into the service container and informing Laravel how to load package resources such as config.
In this 15 minute video, I'll show you how I developed a simple Laravel wrapper package that makes calls to an external API. Directly support these videos: h. Laravel is a web application framework with expressive, elegant syntax. We believe development must be an enjoyable and creative experience to be truly fulfilling. Laravel attempts to take the pain out of development by easing common tasks used in most web projects.
Building A Command
Generating The Class
To create a new command, you may use the
make:console Artisan command, which will generate a command stub to help you get started:
Generate A New Command Class
The command above would generate a class at
When creating the command, the
--command option may be used to assign the terminal command name:
Writing The Command
Once your command is generated, you should fill out the
description properties of the class, which will be used when displaying your command on the
fire method will be called when your command is executed. You may place any command logic in this method.
Arguments & Options
getOptions methods are where you may define any arguments or options your command receives. Both of these methods return an array of commands, which are described by a list of array options.
Laravel Package Development Example
arguments, the array definition values represent the following:
Laravel Package Development Process
mode may be any of the following:
options, the array definition values represent the following:
For options, the argument
mode may be:
VALUE_IS_ARRAY mode indicates that the switch may be used multiple times when calling the command:
Would then allow for this command:
VALUE_NONE option indicates that the option is simply used as a 'switch':
While your command is executing, you will obviously need to access the values for the arguments and options accepted by your application. To do so, you may use the
Retrieving The Value Of A Command Argument
Retrieving All Arguments
Retrieving The Value Of A Command Option
Retrieving All Options
To send output to the console, you may use the
error methods. Each of these methods will use the appropriate ANSI colors for their purpose.
Sending Information To The Console
Sending An Error Message To The Console
You may also use the
confirm methods to prompt the user for input:
Asking The User For Input
Asking The User For Secret Input
Asking The User For Confirmation
You may also specify a default value to the
confirm method, which should be
Calling Other Commands
Sometimes you may wish to call other commands from your command. You may do so using the
Registering An Artisan Command
Once your command is finished, you need to register it with Artisan so it will be available for use. This is typically done in the
app/Console/Kernel.php file. Within this file, you will find a list of commands in the
commands property. To register your command, simply add it to this list.
When Artisan boots, all the commands listed in this property will be resolved by the service container and registered with Artisan.