Chapters
23 Chapters
|
2:20:28
|
Login to bookmark this video
-
Course Code
Subscribe to download the code!
Subscribe to download the code!
-
This Video
Subscribe to download the video!
Subscribe to download the video!
-
Subtitles
Subscribe to download the subtitles!
Subscribe to download the subtitles!
-
Course Script
Subscribe to download the script!
Subscribe to download the script!
22.
Playing with a Custom Console Command
Scroll down to the script below, click on any sentence (including terminal blocks) to jump to that spot in the video!
Subscribe to jump to this part in the video!
Keep on Learning!
If you liked what you've learned so far, dive in! Subscribe to get access to this tutorial plus video, code and script downloads.
This tutorial also works great for Symfony 6!
What PHP libraries does this tutorial use?
// composer.json
{
"require": {
"php": "^7.3.0 || ^8.0.0",
"ext-ctype": "*",
"ext-iconv": "*",
"composer/package-versions-deprecated": "^1.11", // 1.11.99
"knplabs/knp-markdown-bundle": "^1.8", // 1.9.0
"sensio/framework-extra-bundle": "^6.0", // v6.2.1
"sentry/sentry-symfony": "^4.0", // 4.0.3
"symfony/asset": "5.0.*", // v5.0.11
"symfony/console": "5.0.*", // v5.0.11
"symfony/debug-bundle": "5.0.*", // v5.0.11
"symfony/dotenv": "5.0.*", // v5.0.11
"symfony/flex": "^1.3.1", // v1.21.6
"symfony/framework-bundle": "5.0.*", // v5.0.11
"symfony/monolog-bundle": "^3.0", // v3.6.0
"symfony/profiler-pack": "*", // v1.0.5
"symfony/routing": "5.1.*", // v5.1.11
"symfony/twig-pack": "^1.0", // v1.0.1
"symfony/var-dumper": "5.0.*", // v5.0.11
"symfony/webpack-encore-bundle": "^1.7", // v1.8.0
"symfony/yaml": "5.0.*" // v5.0.11
},
"require-dev": {
"symfony/maker-bundle": "^1.15", // v1.23.0
"symfony/profiler-pack": "^1.0" // v1.0.5
}
}
8 Comments
Can u tell me few ideas how can i use commands in my project? I cannot imagine how can I really use them
Hey HudyWeas,
Well, if you don't have any ideas how to use them - probably you don't need them ;) And it's ok, not every project have to use custom command. Well, to get some ideas - you can run bin/console list to see the list of all the available commands out of the box, try to look what they are and maybe you will get with some custom ideas. Also, command are very useful when you need to run some tasks in Cron - you write a command, and tell the Cron job to run it every hour, or day, or month, e.g. you can generate some PDF reports nightly and them to your inbox, or create a custom command migration that you will run on production and that will migrate some heavy data in the background, or even a command that will find all the customers that have active subscription which will be renewed in 7 days and send them email notifications about they will be charged soon, etc.
But once again, you don't have to use them in your project if you don't need :)
Cheers!
Thanks for your answer. Im not doing any project right now. By "my" I meant a project hypothetically. My point is that it's hard to get possible ideas how I can use commands if I don't know real potential of them. This example from video was really easy, it shows bases of commands but I missed some real app examples how really commands may be used
Hey HudyWeas,
Ah, yes, I see... I hope a few my examples above helps ;)
Cheers!
Note : in commands, the default logger is $consoleLogger. You can check this by throwing an error in your command and look at which channel is used. So if you inject a logger in your command, it is better to use LoggerInterface $consoleLogger
Hey Antoine R.!
An, that's an excellent point! For others reading this, the importance of that is that you may have your "logger" channel logger configured to save those logs in different ways than you main logger :).
Cheers!
Hi, is the Console component a Command design pattern? If yes, is there ability to undo command in that component?
Hey Boran Alsaleh
That's a really good question. In my opinion, yes, the Console component implements the command pattern. All the console commands you create are handled the same.
About the undo action. I believe you can create an abstract
AbstractUndoCommandclass which will force all of its derivatives to implement theundo()method, it should also check for a flag to determine if you want to execute the undo method or just run the command. So, whenever you create a command that it's "undoable", it should extend from that abstract class and implement both methodexecuteandundoCheers!
"Houston: no signs of life"
Start the conversation!