我在bundle services.xml文件中设置了3个简单的控制器,如下所示:
http://symfony.com/schema/dic/services/services-1.0.xsd">
<parameters>
<parameter key="wsh_lapi.content.class">Wsh\LapiBundle\Controller\ContentController</parameter>
<parameter key="wsh_lapi.users.class">Wsh\LapiBundle\Controller\UserController</parameter>
<parameter key="wsh_lapi.alerts.class">Wsh\LapiBundle\Controller\AlertController</parameter>
</parameters>
<services>
<service id="wsh_lapi.content" class="%wsh_lapi.content.class%">
<argument type="service" id="service_container"/>
</service>
<service id="wsh_lapi.users" class="%wsh_lapi.users.class%">
<argument type="service" id="service_container"/>
</service>
<service id="wsh_lapi.alerts" class="%wsh_lapi.alerts.class%">
<argument type="service" id="service_container"/>
</service>
</services>
<parameters>
<parameter key="wsh_lapi.content.class">Wsh\LapiBundle\Controller\ContentController</parameter>
<parameter key="wsh_lapi.users.class">Wsh\LapiBundle\Controller\UserController</parameter>
<parameter key="wsh_lapi.alerts.class">Wsh\LapiBundle\Controller\AlertController</parameter>
</parameters>
<services>
<service id="wsh_lapi.content" class="%wsh_lapi.content.class%">
<argument type="service" id="service_container"/>
</service>
<service id="wsh_lapi.users" class="%wsh_lapi.users.class%">
<argument type="service" id="service_container"/>
</service>
<service id="wsh_lapi.alerts" class="%wsh_lapi.alerts.class%">
<argument type="service" id="service_container"/>
</service>
</services>
但是当我尝试在AlertController中获取wsh_lapi.users服务时,如下所示:
class AlertController extends Controller
{
protected $container;
function __construct(Container $container)
{
$this->container = $container;
}
public function postAlert($appIdToken, $securityToken, $searchParams)
{
// first let see if user not allready registered
$em = $this->getDoctrine()->getManager();
//$user = $this->container->get('wsh_lapi.users')->getUser($appIdToken, $securityToken);
if($this->container->has('wsh_lapi.users')) {
// this throws FatalErrorExecption
$userService = $this->container->get('wsh_lapi.users');
}
// check if that alert does not exist allready
$alertRepo = $em->getRepository('WshLapiBundle:Alert');
// todo: do checking
// create new alert object
$alert = new Alert();
$alert->setUser($user);
$alert->setSearchQueryParams($searchParams);
$em->persist($alert);
$em->flush();
return $alert;
}
我收到一个奇怪的错误:
FatalErrorException:错误:在/Users/bard/Projects/LavelAPI/src/Wsh/LapiBundle/Controller/UserController.php第82行中找不到类'Symfony \ Component \ Debug \ Exception \ ContextErrorException'
我不知道该怎么办,第82行是UserController的结尾,看起来像这样:
function __construct(Container $container)
{
$this->container = $container;
}
public function postAlert($appIdToken, $securityToken, $searchParams)
{
// first let see if user not allready registered
$em = $this->getDoctrine()->getManager();
//$user = $this->container->get('wsh_lapi.users')->getUser($appIdToken, $securityToken);
if($this->container->has('wsh_lapi.users')) {
// this throws FatalErrorExecption
$userService = $this->container->get('wsh_lapi.users');
}
// check if that alert does not exist allready
$alertRepo = $em->getRepository('WshLapiBundle:Alert');
// todo: do checking
// create new alert object
$alert = new Alert();
$alert->setUser($user);
$alert->setSearchQueryParams($searchParams);
$em->persist($alert);
$em->flush();
return $alert;
}
}
class UserController extends Controller
{
protected $container;
答案 0 :(得分:1)
事实证明,问题是在我的wsh_lapi.users服务控制器中使用了“getUser”函数的名称。可能是与Symfony基本控制器的某种冲突。在我简单地更改功能名称后,它开始工作。