简体   繁体   English

如何在Symfony3中的app控制器中运行bundle命令?

[英]How to run command from bundle in app controller in Symfony3?

INTRODUCTION 介绍

In my personal project I am using: 在我使用的个人项目中:

  1. Symfony v3.2.7 Symfony v3.2.7
  2. PHP v7.1.1 PHP v7.1.1
  3. CravlerMaxMindGeoIpBundle CravlerMaxMindGeoIpBundle
  4. How to Call a Command from a Controller 如何从控制器调用命令
  5. On Windows 10 Pro dev machine 在Windows 10 Pro开发机器上

TARGET 目标

I would like to run CravlerMaxMindGeoIpBundle's command php bin/console cravler:maxmind:geoip-update from controller successfully. 我想成功运行CravlerMaxMindGeoIpBundle's命令php bin/console cravler:maxmind:geoip-update from controller。

PROBLEM 问题

At the moment I have set up CravlerMaxMindGeoIpBundle bundle and command php bin/console cravler:maxmind:geoip-update works fine in command line. 目前我已经设置了CravlerMaxMindGeoIpBundle包和命令php bin/console cravler:maxmind:geoip-update在命令行中运行正常。

Then I followed official documentation (4th link in intro section). 然后我按照官方文档(介绍部分的第4个链接)。 Changed callable command of course. 当然改变了可调用的命令。 And yet I get an error. 然而我收到了一个错误。

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

QUESTION

What should I do to run the command without an error? 如何在没有错误的情况下运行命令该怎么办?

CODE

My action in controller 我在控制器中的动作

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

My AppKernel with enabled bundles 我的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');
    }
}

FINALLY 最后

What am I missing? 我错过了什么?

It is a common mistake to import: 导入是一个常见的错误:

use Symfony\Component\Console\Application;

instead of: 代替:

use Symfony\Bundle\FrameworkBundle\Console\Application;

So make sure to import the second class (from FrameworkBundle) to load all configured commands (external or not) correctly. 因此,请确保导入第二个类(来自FrameworkBundle)以正确加载所有已配置的命令(外部或非外部)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM