0

I had setup 3 simple controllers in my bundle services.xml file like so:

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>

But when I try to get wsh_lapi.users service in AlertController like so:

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;

}

}

I get a strange error:

FatalErrorException: Error: Class 'Symfony\Component\Debug\Exception\ContextErrorException' not found in /Users/bard/Projects/LavelAPI/src/Wsh/LapiBundle/Controller/UserController.php line 82

I don't know what to do, line 82 is the end of UserController, with looks like this:

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

}

1 Answer 1

1

It turned out that the probles was with the name of function "getUser" in my wsh_lapi.users service controller. Probably it was some kind of collision with Symfony base controller. After I simply change function name it start to work.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.