Last active
December 14, 2016 23:00
-
-
Save nixon1333/c7acd0ea56126cc5de231d2a82947e1f to your computer and use it in GitHub Desktop.
Simple CLI Command using Symfony
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
There is lack of
$
character on line 36. and 48.