when i try to create a new User using POST from angular seems like symfony take the method like GET but i declared POST and didn't add parameters
angular service
CreateUser(body:any){
return this.http.post(environment.url 'registro/nuevo' , body , {responseType:'text'})
.toPromise().then((res:any)=>{
console.log(res)
})
}
symfony controller
/**
* @Route("/registro/nuevo", name="_nuevo" , methods={"POST"})
*/
public function CreateUser(Request $request , EntityManagerInterface $em , UserPasswordEncoderInterface $passwordEncoder ):Response
{
$request = $this->transformJsonBody($request);
$user = new User();
$user->setUsername($request->get("username"));
$user->setPassword( $passwordEncoder->encodePassword($user , $request->get("password")) );
$em->persist($user);
$em->flush();
return new Response(
'usuario creado con exito',
Response::HTTP_OK
);
}
debug:router i can see POST /registro/nuevo
but when i run php bin/console router:match /registro/nuevo
None of the routes match the path "/registro/nuevo"
output
Route "a_registrar" almost matches but method "GET" does not match any of the required methods (POST)
thanks for your answer , when i run $ bin/console router:match /registro/nuevo --method POST the output is [ERROR] None of the routes match the path "/registro/nuevo" , in Network shows meghod is POST , problem is not request , problem is Symfony takes the method of that route as GET when i specified POST , this problem start when i created security bundle and i specified routes starting "/api/" in security.yaml , after that only routes start "/api" can be set with POST methods and routes startting witthout "/api" only GET method may be allowed , for the moment i am registering new users with GET methods passing username and btoa(password) and in php
$user->setPassword($passwordEncoder->encodePassword($user , (base64_decode($password))));
but i dont really like that
CodePudding user response:
what worked for me , is creating another access for the role AUTHENTICATED_ANONYMOUSLY in security.yaml->access_control
access_control:
- { path: ^/api/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/api/registro, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/api, roles: IS_AUTHENTICATED_FULLY }
# - { path: ^/admin, roles: ROLE_ADMIN }
# - { path: ^/profile, roles: ROLE_USER }
and a new route in routes.yaml named SecurityController
SecurityController:
controller: App\Controller\SecurityController::CreateNewUser
path: /api/registro/nuevo
after that my route matched with POST method and finally could register with method POST
So my conclusion is, if I set a new route that is not defined in access control I only can use that route with GET method , but for the access with role IS_AUTHENTICATED_FULLY , I only can use this resource with token .