【发布时间】:2014-11-05 20:57:15
【问题描述】:
试图将 Doctrine dbal 连接传递给我在控制器中的构造。
我正在关注此链接,但它不起作用:
How do you access Doctrine DBAL in a Symfony2 service class?
这是我在 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::__construct() 的参数 1 必须是 Doctrine\DBAL\Connection 的实例,没有给出,在 /srv 中调用/test/tmp/dev/cache/classes.php 在第 2449 行并在 /vagrant/test.com/src/Test/TestBundle/Controller/FormController.php 中定义
这里是 app/config/routing.yml
test_form:
pattern: /api/v4/forms.{_format}
defaults: { _controller: TestTestBundle:Form:cget, _format: json }
requirements:
_method: GET
任何帮助都会很棒。谢谢
【问题讨论】:
-
这个控制器是如何创建的?带有控制器即服务的路由?还是一个新的 FormController()?还是直接从容器中获取?
-
它由 app/console 命令行创建。此控制器的路由在 routing.yml 中,它是到 /api/v4/forms 的修改路由。{_format} 会不会是服务混淆了,找不到该路由?
-
你能从 routing.yml 发布相关行吗?如果它有类似 _controller: TestBundle::Form 的东西,那么这将解释问题。它应该有 __controller: form1.
-
ok 刚刚做了,也许您可以看到问题所在。我将需要使用 /api/v4/forms.{format} 的特定模式,所以不知道是否有另一种方法可以做到这一点,并且在 FormController 下的该方法上保持格式为 json。谢谢
标签: php symfony doctrine-orm fosrestbundle