嗨,我正在使用API 平台。基于 symfony 的 API 框架。
我已经创建了实体并使用@ApiResource
注释公开了它们,因此我可以在 CRUD 的 API 文档中看到它们。
我想在 swagger api 文档中添加一个自定义控制器。例如我的身份验证控制器。
我怎样才能做到这一点?
我的令牌控制器:
class TokenController extends Controller
{
/**
* @Route(path="/auth", name="auth")
*
* @param Request $request
* @return JsonResponse
* @throws \Lexik\Bundle\JWTAuthenticationBundle\Exception\JWTEncodeFailureException
*/
public function tokenAction(Request $request)
{
$em = $this->getDoctrine()->getManager();
$user = $em->getRepository(User::class)->findOneBy(array(
'username' => $request->getUser(),
));
if(!$user){
throw $this->createNotFoundException('No user');
}
$isValid = $this->get('security.password_encoder')->isPasswordValid($user, $request->getPassword());
if(!$isValid){
throw new BadCredentialsException();
}
$token = $this->get('lexik_jwt_authentication.encoder')->encode(array(
'id' => $user->getId(),
'username' => $user->getUsername(),
'roles' => $user->getRoles(),
));
return new JsonResponse(array(
'token' => $token
));
}
}