0

我在我的 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>

但是当我尝试像这样在 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的结尾,看起来像这样:

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;
}

}

4

1 回答 1

1

事实证明,问题出在我的 wsh_lapi.users 服务控制器中的函数“getUser”的名称上。可能是与 Symfony 基本控制器的某种冲突。在我简单地更改函数名称后,它开始工作。

于 2013-07-02T08:20:27.040 回答