2017-07-29 55 views
1

我有这样的代码来控制我得到的帮助来自symfony document 运行模式更新命令我有这样的代码:调用来自控制器的命令不行

namespace AdminBundle\Controller; 

use Symfony\Bundle\FrameworkBundle\Console\Application; 
use Symfony\Bundle\FrameworkBundle\Controller\Controller; 
use Symfony\Component\Console\Input\ArrayInput; 
use Symfony\Component\Console\Output\NullOutput; 
use Symfony\Component\HttpFoundation\Response; 
use Symfony\Component\HttpKernel\KernelInterface; 
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; 

class DefaultController extends Controller 
{ 
    /** 
    * @Route("/admin") 
    */ 
    public function indexAction(KernelInterface $kernel) 
    { 
     $application = new Application($kernal); 

     $input = new ArrayInput(array(
      'command' => 'doctrine:schema:update' 
     )); 

     $output = new NullOutput(); 
     $application->run($input, $output); 

     return new Response(""); 
    } 
} 

它不是我的工作,我得到这个错误后打开此URL(http://127.0.0.1:8000/admin):

Controller "AdminBundle\Controller\DefaultController::indexAction()" requires that you provide a value for the "$kernel" argument. Either the argument is nullable and no null value has been provided, no default value has been provided or because there is a non optional argument after this one. 

我该怎么办?

+0

您使用的是什么symfony版本? –

+0

@DanCostinel版本3.3.5 –

+0

这不是为什么事情没有起作用,但是你有一个错字,并且在Controller动作中有'$ kernal'而不是'$ kernel'。 –

回答

3

而是注入KernelInterface $kernel直接进入你的行动(我猜,你不使用它作为申报服务),直接打电话给你的容器请求内核:

public function indexAction() 
{ 
    $kernel = $this->get('kernel'); 
    $application = new Application($kernel); // btw: you have an typo in here ($kernal vs $kernel) 

    $input = new ArrayInput(array(
     'command' => 'doctrine:schema:update' 
    )); 

    $output = new NullOutput(); 
    $application->run($input, $output); 

    // tip: use "204 No Content" to indicate "It's successful, and empty" 
    return new Response("", 204); 
} 
+0

感谢他的工作对我来说 –

2

虽然迈克尔的回答作品,它不是Symfony 3.3中的首选方法,它对dependency injection进行了几处更改。您的代码实际上可以正常工作,并对您的服务配置进行一些更改。

随着文档状态中,依赖注入容器中的Symfony 3.3改变,并且在默认情况下你的controllers are registered as services

# app/config/services.yml 
services: 
    # ... 

    # controllers are imported separately to make sure they're public 
    # and have a tag that allows actions to type-hint services 
    AppBundle\Controller\: 
     resource: '../../src/AppBundle/Controller' 
     public: true 
     tags: ['controller.service_arguments'] 

这可以让你自动装配通过参数的内核在控制器的操作方法,像你这样的尝试。你的工作不正常的原因是因为你的AdminBundle很可能没有像AppBundle默认的app/config/services.yml那样设置。要真正解决这个问题,在Symfony的3.3希望的方式,你应该添加AdminBundle到您的服务配置,如下所示:

# app/config/services.yml 
services: 
    # add this below your AppBundle\Controller definition 
    AdminBundle\Controller\: 
     resource: '../../src/AdminBundle/Controller' 
     public: true 
     tags: ['controller.service_arguments'] 

有了这一点,你再也不用打电话$this->get('kernel');,和你原来的代码将作为你有它,用KernelInterface作为你的行动方法的参数。

此外,还可以扩展新AbstractController,而不是常规控制器,然后调用$this->get()就不管用了,这是的Symfony的必经之路。

所以,迈克尔的答案也可以正常工作,我会建议你实现我给出的答案,因为Symfony 3.3更喜欢这种方法。

+0

这也是正确的但是由于作者没有提供Symfony的版本,所以我提出了一个答案,这个答案从2.3开始工作:-) –

+0

是的,这两个工作都很棒:)他在后来的评论中加入了这个版本,这就是为什么我提供了替代方案。 –