【问题标题】:How to run command from bundle in app controller in Symfony3?如何在 Symfony3 的应用程序控制器中从 bundle 运行命令?
【发布时间】:2017-09-22 21:28:33
【问题描述】:

简介

在我正在使用的个人项目中:

  1. Symfony v3.2.7
  2. PHP v7.1.1
  3. CravlerMaxMindGeoIpBundle
  4. How to Call a Command from a Controller
  5. 在 Windows 10 专业版开发机器上

目标

我想从控制器成功运行CravlerMaxMindGeoIpBundle's 命令php bin/console cravler:maxmind:geoip-update

问题

目前我已经设置了CravlerMaxMindGeoIpBundle 捆绑和命令php bin/console cravler:maxmind:geoip-update 在命令行中工作正常。

然后我关注了官方文档(介绍部分中的第 4 个链接)。当然改变了可调用的命令。然而我得到了一个错误。

[Symfony\Component\Console\Exception\CommandNotFoundException]
There are no commands defined in the "cravler:maxmind" namespace.

问题

我应该怎么做才能运行命令没有错误?

代码

我在控制器中的操作

public function geoIpUpdateAction(Request $request)
{
    $kernel = $this->get('kernel');
    $application = new Application($kernel);
    $application->setAutoExit(false);

    $input = new ArrayInput(array(
        'command' => 'cravler:maxmind:geoip-update'
    ));
    // You can use NullOutput() if you don't need the output
    $output = new BufferedOutput();
    $application->run($input, $output);

    // return the output, don't use if you used NullOutput()
    $content = $output->fetch();

    // return new Response(""), if you used NullOutput()
    dump($content);

    return $this->render('admin/geo_ip.html.twig');
}

我的 AppKernel 已启用捆绑包

<?php

use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\Config\Loader\LoaderInterface;

class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = [
            new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
            new Symfony\Bundle\SecurityBundle\SecurityBundle(),
            new Symfony\Bundle\TwigBundle\TwigBundle(),
            new Symfony\Bundle\MonologBundle\MonologBundle(),
            new Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle(),
            new Doctrine\Bundle\DoctrineBundle\DoctrineBundle(),
            new Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle(),
            new AppBundle\AppBundle(),
            new Bmatzner\FoundationBundle\BmatznerFoundationBundle(),
            new Knp\Bundle\TimeBundle\KnpTimeBundle(),
            new FOS\UserBundle\FOSUserBundle(),
            new Cravler\MaxMindGeoIpBundle\CravlerMaxMindGeoIpBundle(),
        ];

        if (in_array($this->getEnvironment(), ['dev', 'test'], true)) {
            $bundles[] = new Symfony\Bundle\DebugBundle\DebugBundle();
            $bundles[] = new Symfony\Bundle\WebProfilerBundle\WebProfilerBundle();
            $bundles[] = new Sensio\Bundle\DistributionBundle\SensioDistributionBundle();
            $bundles[] = new Sensio\Bundle\GeneratorBundle\SensioGeneratorBundle();
        }

        return $bundles;
    }

    public function getRootDir()
    {
        return __DIR__;
    }

    public function getCacheDir()
    {
        return dirname(__DIR__).'/var/cache/'.$this->getEnvironment();
    }

    public function getLogDir()
    {
        return dirname(__DIR__).'/var/logs';
    }

    public function registerContainerConfiguration(LoaderInterface $loader)
    {
        $loader->load($this->getRootDir().'/config/config_'.$this->getEnvironment().'.yml');
    }
}

终于

我错过了什么?

【问题讨论】:

  • 为什么不使用流程组件? symfony.com/doc/current/components/process.html
  • @Veas 因为我想从我的控制器运行外部包中存在的命令。据我了解,process 组件适用于自己的命令...
  • @Rikijs 真的没关系,流程组件也可以做到。但是你的代码是对的,所以我的问题是:你在 AppKernel 中添加这个包的环境是什么?这个请求的环境是什么?
  • 这个命令好像不存在,因为包没有加载或者有错别字。
  • @Veas 我用AppKernel 文件更新了问题。我尝试在开发机器上使用 app_dev.php 和 app.php。 Dev environment 返回了问题中提到的错误,但 prod 环境显示了我的自定义错误页面。我稍后会为这两种环境添加错误日志。

标签: php symfony command-line


【解决方案1】:

导入是一个常见的错误:

use Symfony\Component\Console\Application;

代替:

use Symfony\Bundle\FrameworkBundle\Console\Application;

因此请确保导入第二个类(来自 FrameworkBundle)以正确加载所有配置的命令(外部或非外部)。

【讨论】:

  • 很好 - 这正是问题所在。使用正确的导入命令有效!谢谢!
猜你喜欢
  • 1970-01-01
  • 2017-01-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-06
相关资源
最近更新 更多