【发布时间】:2017-09-22 21:28:33
【问题描述】:
简介
在我正在使用的个人项目中:
- Symfony v3.2.7
- PHP v7.1.1
- CravlerMaxMindGeoIpBundle
- How to Call a Command from a Controller
- 在 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