php – Symfony2 – 使用Controller自动连接服务
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了php – Symfony2 – 使用Controller自动连接服务,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2809字,纯文字阅读大概需要5分钟。
内容图文
![php – Symfony2 – 使用Controller自动连接服务](/upload/InfoBanner/zyjiaocheng/773/b97e7d309e2c438cbd8f0c36885ef6c6.jpg)
我能够使用Symfony2的新autowire功能将服务注入服务.但是,我无法将服务注入控制器.我不做什么/做错了什么?
这是我的services.yml文件:
services:
home_controller:
class: AppBundle\Controller\HomeController
autowire: true
这是我的ServiceConsumerDemo:
namespace AppBundle\Services;
class ServiceConsumerDemo {
private $serviceDemo;
public function __construct(ServiceDemo $serviceDemo) {
$this->serviceDemo = $serviceDemo;
}
public function getMessage(){
return $this->serviceDemo->helloWorld();
}
}
这是ServiceDemo:
namespace AppBundle\Services;
class ServiceDemo
{
public function helloWorld(){
return "hello, world!";
}
}
这是HomeController(可以工作):
namespace AppBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
class HomeController extends Controller
{
/**
* @Route("/")
*
*/
public function indexAction(Request $request)
{
$message[0] = $this->get('service_consumer_demo')->getMessage();
return new \Symfony\Component\HttpFoundation\JsonResponse($message);
}
}
这是HomeController不起作用
namespace AppBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use AppBundle\Services\ServiceConsumerDemo;
class HomeController extends Controller
{
private $serviceConsumerDemo;
public function __construct(ServiceConsumerDemo $serviceConsumerDemo) {
$this->serviceConsumerDemo = $serviceConsumerDemo;
}
/**
* @Route("/", name="homepage")
*
*/
public function indexAction(Request $request)
{
$message[0] = $this->serviceConsumerDemo->getMessage();
return new \Symfony\Component\HttpFoundation\JsonResponse($message);
}
}
这是抛出的错误:
Catchable Fatal Error: Argument 1 passed to
AppBundle\Controller\HomeController::__construct() must be an instance
of AppBundle\Services\ServiceConsumerDemo, none given,
如何访问Controller中的服务?我知道我需要将控制器声明为服务.所以我在services.yml文件中提到了控制器.还有什么我需要做的吗?
解决方法:
默认情况下,控制器不被视为服务,因此您无法使用它们进行自动装配.
但是,这是一个简单的修复 – 使用服务定义在类上添加@Route注释(即不在类中的操作方法上).
The @Route annotation on a controller class can also be used to assign
the controller class to a service so that the controller resolver will
instantiate the controller by fetching it from the DI container
instead of calling new PostController() itself.
例如:
/**
* @Route(service="app_controller")
*/
class AppController extends Controller
{
private $service;
public function __construct(SomeService $service)
{
$this->service = $service;
}
/** @Route("/") */
public function someAction()
{
$this->service->doSomething();
}
}
并在您的配置中:
app_controller:
class: AppBundle\Controller\AppController
autowire: true
内容总结
以上是互联网集市为您收集整理的php – Symfony2 – 使用Controller自动连接服务全部内容,希望文章能够帮你解决php – Symfony2 – 使用Controller自动连接服务所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。