【问题标题】:Symfony2 custom console commandSymfony2 自定义控制台命令
【发布时间】:2013-10-19 17:02:46
【问题描述】:

我对 Symfony 还是很陌生。我已经设置了一些我在我的在线作品集中编写的组件的演示,我希望每两个小时清除一次演示数据。在我的网络服务器上,我想像这样设置一个 cron 作业:

php app/console portfolio:wipe

我创建了 app/src/MyFreelancer/PortfolioBundle/Command/WipeCommand.php(PortfolioBundle 在 AppKernel.php 中注册),这是它的内容(完全从 http://symfony.com/doc/current/cookbook/console/console_command.html 复制并更改了命名空间和命令名称)。

<?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);
    }
}
?>

但是,当我跑步时

php app/console portfolio:wipe test

我没有得到“Hello test”,而是得到

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

任何帮助将不胜感激。

【问题讨论】:

    标签: symfony console


    【解决方案1】:

    您的命令名称是maintenance:greet,所以请尝试使用php app/console maintenance:greet test 调用它

    对于您的 cron 作业,在调用 php app/console 之前不要忘记切换到 Symfony2 目录。您也可以使用完整路径调用控制台:php /var/www/where/is/symfony/app/console ...

    【讨论】:

      猜你喜欢
      • 2014-02-23
      • 2018-12-21
      • 2013-10-19
      • 2012-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多