Php Symfony2将Dbal作为构造函数上的服务 namespace Test\TestBundle\Controller; use FOS\RestBundle\Controller\FOSRestController; use FOS\RestBundle\Controller\Annotations as Rest; use FOS\RestBundle\Routing\ClassResourceInterface; use FOS\RestBundle\Util\Codes; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpKernel\Exception\HttpException; use Doctrine\DBAL\Connection; class FormController extends FOSRestController implements ClassResourceInterface { private $connection; public function __construct(Connection $dbalConnection) { $this->connection = $dbalConnection; } }

Php Symfony2将Dbal作为构造函数上的服务 namespace Test\TestBundle\Controller; use FOS\RestBundle\Controller\FOSRestController; use FOS\RestBundle\Controller\Annotations as Rest; use FOS\RestBundle\Routing\ClassResourceInterface; use FOS\RestBundle\Util\Codes; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpKernel\Exception\HttpException; use Doctrine\DBAL\Connection; class FormController extends FOSRestController implements ClassResourceInterface { private $connection; public function __construct(Connection $dbalConnection) { $this->connection = $dbalConnection; } },php,symfony,doctrine-orm,fosrestbundle,Php,Symfony,Doctrine Orm,Fosrestbundle,试图将dbal连接传递到控制器中的我的构造 namespace Test\TestBundle\Controller; use FOS\RestBundle\Controller\FOSRestController; use FOS\RestBundle\Controller\Annotations as Rest; use FOS\RestBundle\Routing\ClassResourceInterface; use FOS\RestBundle\Util\Codes; use Sym

试图将dbal连接传递到控制器中的我的构造

namespace Test\TestBundle\Controller;

use FOS\RestBundle\Controller\FOSRestController;
use FOS\RestBundle\Controller\Annotations as Rest;
use FOS\RestBundle\Routing\ClassResourceInterface;
use FOS\RestBundle\Util\Codes;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Doctrine\DBAL\Connection;


class FormController extends FOSRestController implements ClassResourceInterface
{

    private $connection;

    public function __construct(Connection $dbalConnection)  {
        $this->connection = $dbalConnection;    
    }
}
我正按照此链接进行操作,但它不起作用:

这是app/config/config.yml中的我的服务

services:
 form1:
   class:       Test\TestBundle\Controller\FormController
   arguments:   [@doctrine.dbal.form1_connection]
这是我的控制器和构造
namespace Test\TestBundle\Controller;

use FOS\RestBundle\Controller\FOSRestController;
use FOS\RestBundle\Controller\Annotations as Rest;
use FOS\RestBundle\Routing\ClassResourceInterface;
use FOS\RestBundle\Util\Codes;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Doctrine\DBAL\Connection;


class FormController extends FOSRestController implements ClassResourceInterface
{

    private $connection;

    public function __construct(Connection $dbalConnection)  {
        $this->connection = $dbalConnection;    
    }
}
我得到这个错误“消息”:“可捕获的致命错误:传递给Test\TestBundle\Controller\FormController::u construct()的参数1必须是一个doctor\DBAL\Connection的实例,没有给定,在第2449行的/srv/Test/tmp/dev/cache/classes.php中调用,并在/vagrant/Test.com/src/Test/TestBundle/Controller/FormController.php中定义

namespace Test\TestBundle\Controller;

use FOS\RestBundle\Controller\FOSRestController;
use FOS\RestBundle\Controller\Annotations as Rest;
use FOS\RestBundle\Routing\ClassResourceInterface;
use FOS\RestBundle\Util\Codes;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Doctrine\DBAL\Connection;


class FormController extends FOSRestController implements ClassResourceInterface
{

    private $connection;

    public function __construct(Connection $dbalConnection)  {
        $this->connection = $dbalConnection;    
    }
}
这里是app/config/routing.yml

test_form:
    pattern:  /api/v4/forms.{_format}
    defaults: { _controller: TestTestBundle:Form:cget, _format: json }
    requirements:
        _method: GET

任何帮助都会很好。thx我不知道你的服务,也许你需要这个:doctor.dbal.default\u connection。

好的。基本问题是_controller:testbundle:Form:cget不使用服务容器创建FormController。因此没有注入。你需要将控制器配置为服务

您的路由将如下所示:

test_form:
    pattern:  /api/v4/forms.{_format}
    defaults: { _controller: form1:cgetAction, _format: json }
    requirements:
        _method: GET
但是,这将产生第二个问题,因为您的控制器最终从Symfony base控制器扩展而来,这也需要注入容器。因此,您需要将服务文件调整为类似以下内容:

services:
    form1:
        class:       Test\TestBundle\Controller\FormController
        arguments:   ['@doctrine.dbal.form1_connection']
        calls:
            - [setContainer, ['@service_container']]

我想这应该可以让您继续。

我有两个连接,每个连接都与entity manager连接,一个是form1,另一个是默认的。它们都不能作为服务使用。这个控制器是如何创建的?一个将控制器作为服务的路由?或者一个新的FormController()?或直接从容器?它是由app/console命令行创建的。此控制器的路由在routing.yml中,它是到/api/v4/forms的修改路由。{u format}可能是服务被搞糊涂了,找不到那个路由吗?你能从routing.yml发布相关的行吗?如果它有类似于_controller:TestBundle::Form的东西,那么就可以解释问题了。它应该有_controller:form1。好的,刚才做了,也许你可以看到问题所在。我需要使用一个特定的/api/v4模式/forms.{format}所以不知道是否有其他方法可以做到这一点,也不知道在FormController下该方法上保持json格式。Thx刚刚尝试过,但给了我错误,“消息”:“您请求了一个不存在的服务\“form1\”。您的意思是:\“doctrine.dbal.logger.profiling.form1\”?“,“class:“Symfony\\Component\\DependencyInjection\\Exception\\ServiceNotFoundException”,刚刚发现错误,它缺少以下内容:默认值:{U控制器:form1:cgetAction,\ U格式:json}所以只要编辑一下就好了:),thxMy bad。我总是忘记动作后缀。这是一个好问题,更多的ppl应该看看你的答案:)好的。我非常喜欢将控制器定义为服务。然而,在这种特殊情况下,既然你需要从RestController扩展,那么你已经有了容器,这意味着你已经有了控制器的操作只需使用$this->get('doctrine.dbal.form1_connection');只有当您将控制器设计为不需要容器时,使用服务才能获得回报。