Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
558 views
in Technique[技术] by (71.8m points)

php - How to run command from bundle in app controller in Symfony3?

INTRODUCTION

In my personal project I am using:

  1. Symfony v3.2.7
  2. PHP v7.1.1
  3. CravlerMaxMindGeoIpBundle
  4. How to Call a Command from a Controller
  5. On Windows 10 Pro dev machine

TARGET

I would like to run CravlerMaxMindGeoIpBundle's command php bin/console cravler:maxmind:geoip-update from controller successfully.

PROBLEM

At the moment I have set up CravlerMaxMindGeoIpBundle bundle and command php bin/console cravler:maxmind:geoip-update works fine in command line.

Then I followed official documentation (4th link in intro section). Changed callable command of course. And yet I get an error.

[SymfonyComponentConsoleExceptionCommandNotFoundException]
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

<?php

use SymfonyComponentHttpKernelKernel;
use SymfonyComponentConfigLoaderLoaderInterface;

class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = [
            new SymfonyBundleFrameworkBundleFrameworkBundle(),
            new SymfonyBundleSecurityBundleSecurityBundle(),
            new SymfonyBundleTwigBundleTwigBundle(),
            new SymfonyBundleMonologBundleMonologBundle(),
            new SymfonyBundleSwiftmailerBundleSwiftmailerBundle(),
            new DoctrineBundleDoctrineBundleDoctrineBundle(),
            new SensioBundleFrameworkExtraBundleSensioFrameworkExtraBundle(),
            new AppBundleAppBundle(),
            new BmatznerFoundationBundleBmatznerFoundationBundle(),
            new KnpBundleTimeBundleKnpTimeBundle(),
            new FOSUserBundleFOSUserBundle(),
            new CravlerMaxMindGeoIpBundleCravlerMaxMindGeoIpBundle(),
        ];

        if (in_array($this->getEnvironment(), ['dev', 'test'], true)) {
            $bundles[] = new SymfonyBundleDebugBundleDebugBundle();
            $bundles[] = new SymfonyBundleWebProfilerBundleWebProfilerBundle();
            $bundles[] = new SensioBundleDistributionBundleSensioDistributionBundle();
            $bundles[] = new SensioBundleGeneratorBundleSensioGeneratorBundle();
        }

        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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It is a common mistake to import:

use SymfonyComponentConsoleApplication;

instead of:

use SymfonyBundleFrameworkBundleConsoleApplication;

So make sure to import the second class (from FrameworkBundle) to load all configured commands (external or not) correctly.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...