How to create a custom Artisan Command in Laravel 5
Several artisan commands come shipped with Laravel 5.
Run php artisan list
and you will get all the artisan commands that you can use.
Some of the most popular ones that are used on a daily basis are:
php artisan serve
, php artisan down
, php artisan up
, php artisan make:migration
.
Introduction
The more your application grows, the more functionalities it possesses, the more you want to automate several tasks.
Let’s learn how to create a custom artisan command to show funny faces in the console.
Let’s give fellow software developers and IT administrators the ability to be entertained when they are bored.
We will call the name of the command faces
.
1. Open your terminal and run this command:
php artisan make:console Faces
This creates a file called Faces.php
in the app/Console/Commands
directory.
Open the file Faces.php
Faces.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
<?php namespace AppConsoleCommands; use IlluminateConsoleCommand; class Faces extends Command { /** * The name and signature of the console command. * * @var string */ protected $signature = 'command:name'; /** * The console command description. * * @var string */ protected $description = 'Command description'; /** * Create a new command instance. * * @return void */ public function __construct() { parent::__construct(); } /** * Execute the console command. * * @return mixed */ public function handle() { // } } |
Now, change the value of $signature
to:
1 |
protected $signature = 'faces'; |
and $description
to:
1 |
protected $description = 'Displays a funny face'; |
2. Let’s create the logic for displaying random faces.
Create a function like so:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
/** * Get a funny face * * @return string */ public function displayRandomFaces() { return Collection::make([ "( .-. )", "( .o.)", "( `·´ )", "( ° ͜ ʖ °)", "( ͡° ͜ʖ ͡°)", "( ⚆ _ ⚆ )", "( ︶︿︶)", "( ゚ヮ゚)", "(\/)(°,,,°)(\/)", "(¬_¬)", "(¬º-°)¬", "(¬‿¬)", "(°ロ°)☝", "(´・ω・)っ", "(ó ì_í)", "(ʘᗩʘ')", "(ʘ‿ʘ)", "(̿▀̿ ̿Ĺ̯̿̿▀̿ ̿)̄", "(͡° ͜ʖ ͡°)", "(ಠ_ಠ)", "(ಠ‿ಠ)", "(ಠ⌣ಠ)", "(ಥ_ಥ)", "(ಥ﹏ಥ)", "(ง ͠° ͟ل͜ ͡°)ง", "(ง ͡ʘ ͜ʖ ͡ʘ)ง", "(ง •̀_•́)ง", "(ง'̀-'́)ง", "(ง°ل͜°)ง", "(ง⌐□ل͜□)ง", "(ღ˘⌣˘ღ)", "(ᵔᴥᵔ)", "(•ω•)", "(•◡•)/", "(⊙ω⊙)", "(⌐■_■)", "(─‿‿─)", "(╯°□°)╯", "(◕‿◕)", "(☞゚∀゚)☞", "(❍ᴥ❍ʋ)", "(っ◕‿◕)っ", "(づ。◕‿‿◕。)づ", "(ノಠ益ಠ)ノ", "(ノ・∀・)ノ", "(;一_一)", "(`◔ ω ◔´)", "(。◕‿‿◕。)", "(ノ◕ヮ◕)ノ", "*<{:¬{D}}}", "=^.^=", "t(-.-t)", "| (• ◡•)|", "~(˘▾˘~)", "¬_¬", "¯(°_o)/¯", "¯_(ツ)_/¯", "°Д°", "ɳ༼ຈل͜ຈ༽ɲ", "ʅʕ•ᴥ•ʔʃ", "ʕ´•ᴥ•`ʔ", "ʕ•ᴥ•ʔ", "ʕ◉.◉ʔ", "ʕㅇ호ㅇʔ", "ʕ;•`ᴥ•´ʔ", "ʘ‿ʘ", "͡° ͜ʖ ͡°", "ζ༼Ɵ͆ل͜Ɵ͆༽ᶘ", "Ѱζ༼ᴼل͜ᴼ༽ᶘѰ", "ب_ب", "٩◔̯◔۶", "ಠ_ಠ", "ಠoಠ", "ಠ~ಠ", "ಠ‿ಠ", "ಠ⌣ಠ", "ಠ╭╮ಠ", "ರ_ರ", "ง ͠° ل͜ °)ง", "๏̯͡๏﴿", "༼ ºººººل͟ººººº ༽", "༼ ºل͟º ༽", "༼ ºل͟º༼", "༼ ºل͟º༽", "༼ ͡■ل͜ ͡■༽", "༼ つ ◕_◕ ༽つ", "༼ʘ̚ل͜ʘ̚༽", "ლ(´ڡ`ლ)", "ლ(́◉◞౪◟◉‵ლ)", "ლ(ಠ益ಠლ)", "ᄽὁȍ ̪őὀᄿ", "ᔑ•ﺪ͟͠•ᔐ", "ᕕ( ᐛ )ᕗ", "ᕙ(⇀‸↼‶)ᕗ", "ᕙ༼ຈل͜ຈ༽ᕗ", "ᶘ ᵒᴥᵒᶅ", "(ノಥ益ಥ)ノ", "≧☉_☉≦", "⊙▃⊙", "⊙﹏⊙", "┌( ಠ_ಠ)┘", "╚(ಠ_ಠ)=┐", "◉_◉", "◔ ⌣ ◔", "◔̯◔", "◕‿↼", "◕‿◕", "☉_☉", "☜(⌒▽⌒)☞", "☼.☼", "♥‿♥", "⚆ _ ⚆", "✌(-‿-)✌", "〆(・∀・@)", "ノ( º _ ºノ)", "ノ( ゜-゜ノ)", "ヽ( ͝° ͜ʖ͡°)ノ", "ヽ(`Д´)ノ", "ヽ༼° ͟ل͜ ͡°༽ノ", "ヽ༼ʘ̚ل͜ʘ̚༽ノ", "ヽ༼ຈل͜ຈ༽ง", "ヽ༼ຈل͜ຈ༽ノ", "ヽ༼Ὸل͜ຈ༽ノ", "ヾ(⌐■_■)ノ", "꒰・◡・๑꒱", "﴾͡๏̯͡๏﴿", "。◕‿◕。", "ʕノ◔ϖ◔ʔノ", "꒰•̥̥̥̥̥̥̥ ﹏ •̥̥̥̥̥̥̥̥๑꒱", "ಠ_ರೃ", "(ू˃̣̣̣̣̣̣︿˂̣̣̣̣̣̣ ू)", "(ꈨຶꎁꈨຶ)۶”", "(ꐦ°᷄д°᷅)", "(۶ૈ ۜ ᵒ̌▱๋ᵒ̌ )۶ૈ=͟͟͞͞ ⌨", "₍˄·͈༝·͈˄₎◞ ̑̑ෆ⃛", "(*゚⚙͠ ∀ ⚙͠)ノ❣", "٩꒰・ัε・ั ꒱۶", "ヘ(。□°)ヘ", "˓˓(ृ ु ॑꒳’)ु(ृ’꒳ ॑ ृ )ु˒˒˒", "꒰✘Д✘◍꒱", "૮( ᵒ̌ૢཪᵒ̌ૢ )ა", "“ψ(`∇´)ψ", "ಠﭛಠ", "(๑>ᴗ<๑)", "(۶ꈨຶꎁꈨຶ )۶ʸᵉᵃʰᵎ", "٩(•̤̀ᵕ•̤́๑)ᵒᵏᵎᵎᵎᵎ", "(oT-T)尸", "(✌゚∀゚)☞", "ಥ‿ಥ", "ॱ॰⋆(˶ॢ‾᷄﹃‾᷅˵ॢ)", "┬┴┬┴┤ (ಠ├┬┴┬┴", "( ˘ ³˘)♥", "Σ (੭ु ຶਊ ຶ)੭ु⁾⁾", "(⑅ ॣ•͈ᴗ•͈ ॣ)", "ヾ(´¬`)ノ", "(•̀o•́)ง", "(๑•॒̀ ູ॒•́๑)", "⚈้̤͡ ˌ̫̮ ⚈้̤͡", "=͟͟͞͞ =͟͟͞͞ ヘ( ´Д`)ノ", "(((╹д╹;)))", "•̀.̫•́✧", "(ᵒ̤̑ ₀̑ ᵒ̤̑)", "_(ʘ_ʘ)_/" ])->random(); } |
Look at the function carefully. We are using the Collection
Facade and calling the random
method it offers to help us return a random face.
One more thing, we need to require the Collection
class at the top of the file like so:
1 |
use IlluminateSupportCollection; |
To send output to the console, we can use line
, info
, comment
, question
and error
methods from Laravel Command
class.
3. Let’s register the command
Open app/Console/Kernel.php
file and add the name of the class to the $commands
array like so:
1 2 3 4 5 6 7 8 9 10 |
..... /** * The Artisan commands provided by your application. * * @var array */ protected $commands = [ ........, CommandsFaces::class, ]; |
Now, go to the terminal and run php artisan list
, you will see the name of your command listed:
The faces
command is listed with the description. Awesome!
4. Run the command
Now, run php artisan faces
in your terminal.
It shows a different face every time the command is been run in the terminal
Conclusion
Now, we know how to create custom Artisan commands in Laravel 5.
This is barely scratching the surface, there is a lot more you can do. You can programme it to receive input from the console.
You can also program it to receive results from an external service like an API.
You can program it to scan, make, list and delete directories.
It all solely depends on your idea.
NOTE: The link to the source code is here
Please, if you have any question or observations, let me know in the comments section.
- How to build your own Youtube – Part 10 - August 1, 2016
- How to build your own Youtube – Part 9 - July 25, 2016
- How to build your own Youtube – Part 8 - July 23, 2016
- How to build your own Youtube – Part 6 - July 6, 2016
- Introducing Laravel Password v1.0 - July 3, 2016
- How to build your own Youtube – Part 5 - June 28, 2016
- How to build your own Youtube – Part 4 - June 23, 2016
- How to build your own Youtube – Part 3 - June 15, 2016
- How to build your own Youtube – Part 2 - June 8, 2016
- How to build your own Youtube – Part 1 - June 1, 2016