Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2 custom console command

I am still pretty new to Symfony. I have set up 'n demonstration of some of the components that I have written on my online portfolio and I want this demo data to be cleared every two hours. On my web server I want to set a cron job like so:

php app/console portfolio:wipe

I have created app/src/MyFreelancer/PortfolioBundle/Command/WipeCommand.php (PortfolioBundle is registered in AppKernel.php) and here is its contents (copied exactly from http://symfony.com/doc/current/cookbook/console/console_command.html and changed the namespace and command name).

<?php
namespace MyFreelancer\PortfolioBundle\Command;

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class WipeCommand extends ContainerAwareCommand
{
    protected function configure()
    {
        $this
            ->setName('maintenance:greet')
            ->setDescription('Greet someone')
            ->addArgument('name', InputArgument::OPTIONAL, 'Who do you want to greet?')
            ->addOption('yell', null, InputOption::VALUE_NONE, 'If set, the task will yell in uppercase letters');
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $name = $input->getArgument('name');
        if ($name) {
            $text = 'Hello '.$name;
        } else {
            $text = 'Hello';
        }

        if ($input->getOption('yell')) {
            $text = strtoupper($text);
        }

        $output->writeln($text);
    }
}
?>

However, when I run

php app/console portfolio:wipe test

Instead of getting "Hello test", I get

There are no commands defined in the "portfolio" namespace.

Any help would be appreciated.

like image 949
Magnanimity Avatar asked Sep 22 '26 10:09

Magnanimity


1 Answers

Your command name is maintenance:greet, so try to call it with php app/console maintenance:greet test

And for your cron job, don't forget to change to the Symfony2 directory before calling php app/console. You can also call the console with the full path : php /var/www/where/is/symfony/app/console ...

like image 128
A.L Avatar answered Sep 26 '26 01:09

A.L



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!