Skip to content

Instantly share code, notes, and snippets.

@nixon1333
Last active December 14, 2016 23:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nixon1333/c7acd0ea56126cc5de231d2a82947e1f to your computer and use it in GitHub Desktop.
Save nixon1333/c7acd0ea56126cc5de231d2a82947e1f to your computer and use it in GitHub Desktop.
Simple CLI Command using Symfony
<?php
// this is the file structure of the command file.
// src/SmsBundle/Command/SendSmsCommand.php
namespace SmsBundle\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
//we need this to take inputs from the console.
use Symfony\Component\Console\Input\InputArgument;
class SendSmsCommand extends Command {
protected function configure() {
$this
// command name (the part after "app/console")
->setName('sms:send')
// first input for phone number.
// This is mendetory input. Without this it will create error.
->addArgument('phone', InputArgument::REQUIRED, 'Phone number?')
// second input for message.
// Notice that this argument is optional, which means, user can ignore this input
->addArgument('message', InputArgument::OPTIONAL, 'Short Message?')
// the short description shown while running "php app/console list"
->setDescription('Sends a sms to the phone number.')
;
}
protected function execute(InputInterface $input, OutputInterface $output) {
// get the input values using getArgument()
$phoneNumber = $input->getArgument('phone');
$shortMessage = $input->getArgument('message')
// shows a message by adding a "\n"
$output->writeln('Sending a SMS to the number -> ' . phoneNumber);
//also you can send array of lines
$output->writeln([
'----##--------##----',
'',
]);
// ...
// sms sending code
// ...
// shows a message without adding a "\n"
$output->write('SMS Sent -> ' . phoneNumber . '. with a message: ' . $shortMessage);
}
}
?>
@Albert221
Copy link

There is lack of $ character on line 36. and 48.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment