【问题标题】:symfony2 run console command from controllersymfony2 从控制器运行控制台命令
【发布时间】:2014-11-29 03:21:05
【问题描述】:

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

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”命名空间中没有定义命令。

希望有人能帮我解决这个错误。

【问题讨论】:

  • 我不知道你为什么要在这里使用内核。调用命令很简单,stackoverflow.com/questions/10497567/…
  • 我正在开发一个允许用户定义自己的数据模式的项目。因此,我创建了第一步,用户可以通过定义字段和数据类型来实际创建新表。问题是如何允许用户在新表中插入和修改数据。出于这个原因,我想根据来自控制器的新表生成实体。

标签: symfony controller command entity


【解决方案1】:

这就是我为旧版本的 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');
    }
}


解释

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

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

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

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

【讨论】:

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