4

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.

[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

<?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?

7
  • why dont you use the Process Component? symfony.com/doc/current/components/process.html Commented Apr 25, 2017 at 18:23
  • @Veas Because I want to run command that exists in external bundle from my controller. As I understand it, process component is meant for ones own commands... Commented Apr 25, 2017 at 18:34
  • @Rikijs really no matter, the Process Component can do it too. However you code is right, so my question is: What is the environment where you are added this bundle in you AppKernel and what is the environment of this request? Commented Apr 25, 2017 at 18:40
  • This looks like this command doesn't exists because the bundle is not loaded or there is a typo. Commented Apr 25, 2017 at 18:42
  • @Veas I updated question with AppKernel file. I tried with app_dev.php and with app.php on dev machine. Dev environment returned error that is mentioned in the question, but prod environment showed my custom error page. I will add error logs for both environments in a moment. Commented Apr 25, 2017 at 18:55

1 Answer 1

12

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.