我在我的包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>
但是,當我試圖讓在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:錯誤:類「的Symfony \分量\調試\異常\ ContextErrorE xception'找不到在/Users/bard/Projects/LavelAPI/src/Wsh/LapiBundle/Controller/UserController.php線82
我不知道該怎麼做,第82行是UserController的結束,與外觀像這樣:
class UserController extends Controller { protected $container;
function __construct(Container $container)
{
$this->container = $container;
}
/**
* Register new user or return existing one based on given AppId token
* @param $appIdToken unique user token generated in client app
* @return User
*/
public function registerDevice($appIdToken)
{
// first let see if user not allready registered
$em = $this->getDoctrine()->getManager();
$repo = $em->getRepository('WshLapiBundle:User');
$user = $repo->findOneByAppId($appIdToken);
if(!$user) {
// user not found create new one
$user = new User();
$user->setAppId($appIdToken);
$em->persist($user);
$em->flush();
}
return $user;
}
/**
* Removes user from database so he's token will no longer be authenticated
*
* @param $appIdToken
* @param $securityToken
* @return string
* @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
*/
public function unRegisterDevice($appIdToken, $securityToken)
{
$user = $this->getUser($appIdToken, $securityToken);
$em = $this->getDoctrine()->getManager();
$em->remove($user);
$em->flush();
return "OK";
}
public function getUser($appIdToken, $securityToken)
{
// first let see if user not allready registered
$em = $this->getDoctrine()->getManager();
$repo = $em->getRepository('WshLapiBundle:User');
$user = $repo->findOneByAppId($appIdToken);
if(!$user) {
throw $this->createNotFoundException('No user with appIdToken: '.$appIdToken.' has been found');
}
// check security token
if(!$user->checkSecurityToken($securityToken)) {
throw new \Exception('Request not authorized. Tokens does not match');
}
return $user;
}
}