How to create a custom CLI and use it in Symfony (part2)
1- file and folder structure
2- creation of this structure
3- mandatory files for the bundle
Part 2
4- creation of the command :-D
5- push it in the packagist
6- installed it in a Symfony project and run the command
Let’s continue
4- creation of the command
Before creating the command we need symfony console component version 5.3, so let’s run `composer require symfony/console `, after that symfony/console will be installed and you will see it in your composer.json file and the composer.lock file will be created,
In src/Command/CoffeCommand.php we need 2 mendatory functions
/**here we set ower command*/
public static $defaultName = ‘you:make:me:coffee’;
/**here we configure it */
protected function configure(): void
{
}
/**here we execute the code when de command is called */
protected function execute(InputInterface $input, OutputInterface $output): int
{
}
in execute function is where you can do your logic and return what you want, in our case the execute function return a INT value, We will see why !
In ./bin/console we need to instantiate our empty command, of course our command do nothing for the moment
now in terminal run `php bin/console list`, you should see this
let’s add our code inside configure() and execute() functions, to make a delicieuse coffee :
and now locally run `php bin/console you:make:me:coffee`, you will see this :
5- push it in the packagist
1- push your code to github repo [BE CARFULL : change the name of the repo because « coffeeshop/maker-coffee-bundle » is already taken ;-)]
2- signing in packagiste (or create an account)
3- copy / pasta :-p the github url in ‘SUBMIT SECTION’ of your packagist account :
4- after check submit your package: when All it’s done you will get this
Wonderfull you get your package in packagaiste, ignore for the moment the errors & warnings, you can fix it later (StackOverFlow i am invok you now …)
now let’s try to install it in syfmony project, Rhhoooo i can not wait let’s do it
6- installed it in a Symfony project and run the command
1- init an empty Symfony project or use a sandbox symfony project to test (it’s a little ecological)
2- and run composer require name of your package as Packagist have set it
3- play your commands and have fun
Et Voila !
GitHubRepo