【发布时间】:2016-03-26 22:50:30
【问题描述】:
我正在尝试将控制器创建为服务,如 http://symfony.com/doc/current/cookbook/controller/service.html 所示。我已经按照这个示例进行了操作,当我在 app/config/routing.yml 中设置了路由时,一切正常。但是,当我尝试通过注释设置路线时,出现错误
我的 routing.yml 文件如下所示:
#hello:
# path: /hello/{name}
# defaults: { _controller: app.hello_controller:indexAction }
hello:
resource: "@EventBundle/Controller/HelloController.php"
type: annotation
我的控制器如下所示:
<?php
namespace Me\EventBundle\Controller;
//use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
use Symfony\Component\HttpFoundation\Response;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
//class HelloController extends Controller
class HelloController
{
private $templating;
public function __construct(EngineInterface $templating)
{
$this->templating = $templating;
}
/**
* @Route("/hello/{name}", name="hello")
*
*/
public function indexAction($name)
{
return $this->templating->renderResponse(
'EventBundle:Default:test.html.twig',
array('name' => $name)
);
}
}
正如我所说,如果我只使用 routing.yml 而不是页面正确呈现的注释。但是使用注释我得到了错误:
可捕获的致命错误:传递给 Me\EventBundle\Controller\HelloController::__construct() 的参数 1 必须是 Symfony\Bundle\FrameworkBundle\Templating\EngineInterface 的实例,没有给出,在 /Library/WebServer/Documents/ 中调用symfony-project/app/cache/dev/classes.php 在第 2176 行并定义
编辑 - 按照 cmets 的要求:
service.yml 看起来像:
services:
app.hello_controller:
class: Me\EventBundle\Controller\HelloController
arguments: ['@templating']
【问题讨论】:
-
尝试将
controller (service)的ID 添加到您的注释映射中。@Route("/example", service="my_service_id") -
你能发布你的 services.yml 文件吗
-
@SeifSayed 我已经发布了 services.yml。
-
您可以将您的解决方案添加为答案并接受它;)
标签: symfony service controller routing annotations