symfony2从控制器运行控制台命令

时间:2014-11-29 03:21:06

标签: symfony controller command entity

我需要从控制器调用控制台命令来生成新实体。这是我到目前为止的代码:

use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Console\Output\ConsoleOutput;
use Symfony\Component\Console\Application;

public function newAction()
{

    $kernel = $this->container->get('kernel');
    $kernel->shutdown();
    $kernel->boot();
    $app = new Application($kernel);
    $app->setAutoExit(false);

    $input = new \Symfony\Component\Console\Input\ArrayInput(array('command' =>  
        'doctrine:generate:entities', 'name'  => 'AdminBundle','-- o-backup' => 'true'));

    $app->doRun($input, new \Symfony\Component\Console\Output\ConsoleOutput());
}

但这是我得到的错误:

“doctrine:generate”命名空间中没有定义命令。

我希望有人可以帮我解决错误。

1 个答案:

答案 0 :(得分:0)

这就是我为旧版Symfony做的 - 2.0:

<?php
namespace Admin\MyBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Bundle\FrameworkBundle\Console\Application;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;

use Symfony\Component\HttpFoundation\Request;

use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Console\Output\ConsoleOutput;

use Admin\MyBundle\Command;

/**
 * @author Nataliya Naydenova
 *
 * @Route("/cron-manager")
 */
class CronManagerController extends Controller 
{ 

    /**
     * Manually run cron jobs
     *
     * @param $commandName as defined in MyCommand.php
     */
    private function runCommandMannually($commandName)
    {

        $text = 'Starting ...';

        ini_set('max_execution_time', 600);

        $command = new Command\MyCommand();
        $command->setContainer($this->container);

        $arguments = array(
            'command' => 'my:command',
            'name'    => $commandName,
            '--env'  => 'local',
        );

        $input = new ArgvInput($arguments);
        $output = new ConsoleOutput();

        $returnCode = $command->run($input, $output);

        if($returnCode == 0) {
            $text .= 'successfully loaded!';
        } else {
            $text .= 'error!: ' . $returnCode;
        }

        $this->get('session')->setFlash('message', $text);
        return $this->redirect($this->generateUrl('cron_manager'));
    }

    /**
     * Pull first cron
     *
     * @Route("/product-image", name="cron_manager_product_image")
     */
    public function productImageAction()
    {
        self::runCommandMannually('product_image_update');
    }

    /**
     * Pull second cron
     *
     * @Route("/product-info", name="cron_manager_product_info")
     */
    public function productInfoAction()
    {
        self::runCommandMannually('product_info_update');
    }
}


Exlpanation

我想通过此控制器运行的命令位于 Admin / MyBundle / Command / MyCommand.php 类中。

因为它们需要相当长的时间才能执行,所以我也通过ini_set('max_execution_time', 600);增加了最长执行时间。

接下来要做的是为 MyCommand()对象设置容器,然后将所有必要的参数传递给输入:命令本身,名称和环境。
< / p>

然后$command->run()方法运行并在成功时返回0或在失败时返回警告。根据它,我构建我的flash消息并重定向到我控制这些crons的管理器页面。

相关问题