如何使用FOSRestBundle引用路由名称

时间:2016-02-03 02:27:37

标签: symfony fosrestbundle

候选人控制员

class DefaultController extends PreviewMeController
{

    /**
     * Complete registration process for candidate
     *
     * @ApiDoc(
     *     section="Candidate",
     *     tags={"common"},
     * )
     *
     * @Rest\View()
     * @Post("/ua/register/candidate/{token}")
     *
     * @param Request $request
     * @return \FOS\RestBundle\View\View
     */
    public function registerCandidateAction($token)
    {

    }

}

Candidate routing.yml

candidate_api_routes:
    type: rest
    prefix: /v1
    resource: "CandidateBundle\Controller\DefaultController"
    name_prefix: "api_1_c_"

AppBundle控制器

/**
     * Register a new user on the website
     *
     * @ApiDoc(
     *     section="Common Functionalities",
     *     tags={"common"},
     *     requirements={
     *          {"name"="email", "dataType"="string", "description"="Email of user"},
     *          {"name"="username", "dataType"="string", "description"="Username. Keep this same as email address"},
     *          {"name"="first_name", "dataType"="string", "description"="First name of user"},
     *          {"name"="last_name", "dataType"="string", "description"="Last name of user"},
     *          {"name"="plainPassword", "dataType"="array", "requirement"="['first':'password','second':'password']", "description"="Plain password. Send as an array with 'first' and 'second' as array keys"},
     *          {"name"="user_type","dataType"="string","requirement"="employer|candidate","description"="Employer or candidate user type"}
     *     },
     *     statusCodes={
     *          200 = "When user is successfully registered",
     *          400="When there is a validation error in the registration process"
     *     }
     * )
     * @Post("/ua/register")
     * @Rest\View()
     *
     * @param Request $request
     * @return array|\FOS\RestBundle\View\View
     */
    public function registerAction(Request $request)
    {
        /** @var $formFactory \FOS\UserBundle\Form\Factory\FactoryInterface */
        $formFactory = $this->get('fos_user.registration.form.factory');
        /** @var UserManager $fos_userManager */
        $fos_userManager = $this->get('fos_user.user_manager');

        /** @var User $user */
        $user = $fos_userManager->createUser();
        $user->setEnabled(true);
        $user->setUserType($request->request->get('user_type'));
        //remove user_type from request so it's not forwarded to form
        $request->request->remove('user_type');

        $form = $formFactory->createForm();
        $form->setData($user);

        $form->submit($request->request->all());

        if( $form->isValid() ){

            $event = new UserEvent($user);
            $dispatcher = $this->get('event_dispatcher');
            $dispatcher->dispatch(PmEvents::REGISTRATION_SUCCESS, $event);

            $fos_userManager->updateUser($user);

            $wrapper = new PMResponseWrapper();
            $wrapper->setData(array(
                'ob_key' => $user->getObKey()
            ));

            /** @var View $response */
            $response = View::create($wrapper->getFormattedData());
            $response->setLocation( $this->generateUrl('register_candidate') );

            return $response;
        }

        return $this->view($form);

    }

app / console debug:router dump

  api_1_register                 POST       ANY      ANY    /api/v1/ua/register
  api_1_register_confirm_token   POST       ANY      ANY    /api/v1/ua/register/confirm_token/{token}
  api_1_c_index                  GET        ANY      ANY    /api/v1/index
  api_1_c_register_candidate     POST       ANY      ANY    /api/v1/ua/register/candidate/{token}

问题即使registerCandidateAction中显示debug:router,我也无法在$this->generateUrl()中使用registerAction进行调用。

当我拨打此行$response->setNextUrl($this->generateUrl('register_candidate'));时,我收到此错误Unable to generate a URL for the named route \"register_candidate\" as such route does not exist.

请帮助找出这里的错误。

0 个答案:

没有答案