您已请求不存在的服务\" user.handler \"

时间:2016-03-28 08:21:40

标签: symfony

我的服务中有一个错误restuser.handler,其他服务有效,请问

services:
#    rest_user.example:
#        class: %rest_user.example.class%
#        arguments: [@service_id, "plain_value", %parameter%]

  restuser.form.register:
      class: REST\UserBundle\Form\RegisterType
      arguments: [%fos_user.model.user.class%]
      tags:
            - { name: form.type, alias: rest_user_register}

  restuser.form.profile:
      class: REST\UserBundle\Form\ProfileType
      arguments: [%fos_user.model.user.class%]
      tags:
            - { name: form.type, alias: rest_user_profile }


  restuser.handler:
        class: REST\UserBundle\Handler\UserHandler
        arguments: [@doctrine.orm.entity_manager, @form.factory, @security.encoder_factory]

我的控制器如下:

class UserController extends  FOSRestController{

/**
 * @QueryParam(name="offset", requirements="\d+", nullable=true, description="Offset from which to start listing pages.")
 * @QueryParam(name="limit", requirements="\d+", nullable=true, default="20", description="How many pages to return.")
 *
 * @View()
 *
 * @param Request               $request      the request object
 * @param ParamFetcherInterface $paramFetcher param fetcher service
 *
 * @return array
 */
public function getUsersAction(Request $request, ParamFetcherInterface $paramFetcher)
{
    $offset = $paramFetcher->get('offset');
    $offset = null == $offset ? 0 : $offset;
    $limit = $paramFetcher->get('limit');

    return $this->container->get('user.handler')->all($limit, $offset);
}

/**
 * @param User $user
 *
 * @View()
 * @return array
 */
public function getUserAction($id)
{
    $user=$this->getOr404($id);

    return $user;
}

/**
 *
 * @View()
 *
 * @param Request $request
 * @param int     $id
 *
 * @return View
 *
 * @throws NotFoundHttpException when user not exist
 */
public function putUserAction(Request $request, $id)
{
    try {
        if (!($user = $this->container->get('user.handler')->get($id))) {
            $statusCode = Codes::HTTP_CREATED;
            $user = $this->container->get('user.handler')->post($request);
        } else {
            $statusCode = Codes::HTTP_NO_CONTENT;
            $user = $this->container->get('user.handler')->put($user, $request);
        }
        $response = new Response('El usuario ha sido guardado con éxito', $statusCode);

        return $response;
    } catch (\Exception $exception) {

        return $exception->getMessage();
    }
}

/**
 *
 * @View()
 *
 * @param Request $request
 * @param int     $id
 *
 * @return FormTypeInterface|View
 *
 * @throws NotFoundHttpException when user not exist
 */
public function patchUserAction(Request $request, $id)
{
    try {
        if (($user = $this->getOr404($id))) {
            $statusCode = Codes::HTTP_ACCEPTED;
            $user = $this->container->get('user.handler')->patch($user, $request);
        } else {
            $statusCode = Codes::HTTP_NO_CONTENT;
        }
        $response = new Response('El usuario ha sido guardado con éxito', $statusCode);

        return $response;
    } catch (NotFoundHttpException $exception) {

        return $exception->getMessage();
    }
}

/**
 *
 * @View()
 *
 * @param Request $request
 * @param int     $id
 *
 * @return FormTypeInterface|View
 *
 * @throws NotFoundHttpException when user not exist
 */
public function deleteUserAction(Request $request, $id)
{
    if (($user = $this->container->get('user.handler')->get($id))) {
        $statusCode = Codes::HTTP_ACCEPTED;
        $user = $this->container->get('user.handler')->delete($user);
    } else {
        $statusCode = Codes::HTTP_NO_CONTENT;
    }
    $response = new Response('El usuario se ha eliminado', $statusCode);

    return $response;
}

/**
 * Fetch the Page or throw a 404 exception.
 *
 * @param mixed $id
 *
 * @return PageInterface
 *
 * @throws NotFoundHttpException
 */
protected function getOr404($id)
{
    if (!($page = $this->container->get('user.handler')->get($id))) {
        throw new NotFoundHttpException(sprintf('The User \'%s\' was not found.',$id));
    }

    return $page;
}

}

我的类用户处理程序如下:

public function __construct(EntityManager $em, FormFactoryInterface $formFactory, EncoderFactory $encoderFactory)
{
    $this->em = $em;
    $this->factory = $formFactory;
    $this->encoderFactory = $encoderFactory;
}

public function get($id = null, $email = null)
{
    if ($email) {
        return $this->em->getRepository('UserBundle:User')->findOneBy(array('email' => $email));
    }
    return $this->em->getRepository('UserBundle:User')->find($id);
}

/**
 * @param int $limit  the limit of the result
 * @param int $offset starting from the offset
 *
 * @return array
 */
public function all($limit = 20, $offset = 0, $orderby = null)
{
    return $this->em->getRepository('UserBundle:User')->findBy(array(), $orderby, $limit, $offset);
}

/**
 * Create a new User.
 *
 * @param $request
 *
 * @return User
 */
public function post(Request $request)
{
    $user = new User();

    return $this->processForm($user, $request, 'POST');
}

/**
 * @param User $user
 * @param $request
 *
 * @return User
 */
public function put(User $entity, $request)
{
    return $this->processForm($entity, $request);
}

/**
 * @param User $user
 * @param $request
 *
 * @return User
 */
public function patch(User $entity, $request)
{
    return $this->processForm($entity, $request, 'PATCH');
}

/**
 * @param User $user
 *
 * @return User
 */
public function delete(User $entity)
{
    $this->em->remove($entity);
    $this->em->flush($entity);
}

/**
 * Processes the form.
 *
 * @param User     $user
 * @param array         $parameters
 * @param String        $method
 *
 * @return User
 *
 * @throws \Exception
 */
private function processForm(User $entity, Request $request, $method = "PUT")
{
    $form = $this->factory->create(new AppUserType(), $entity, array('method' => $method));
    $form->handleRequest($request);
    if ($form->isValid()) {
        $req = $request->request->get('user');
        if (!$req) {
            $req = $request->request->get('user');
        }
        if ($req['password']!= "") {
            $entity->setPassword($req['password']);
        }
        $this->em->persist($entity);
        $this->em->flush($entity);

        return $entity;
    }

    return View::create($form, 400);
}

我尝试清除缓存并更改服务名称但仍无效,请帮助我

0 个答案:

没有答案