I create a local website to learn Symfony 5 but when I specify method="POST" in my template form I have error
No route found for "GET /pins/create": Method Not Allowed (Allow: POST)
In my controller, I want to get only the method POST but when it's working only if I add GET too
Controller:
/**
* @Route("/pins/create",name="create_pin", methods={"POST"})
*/
public function create(Request $req, EntityManagerInterface $em)
{
if($req->isMethod('POST'))
{
$pins = new Pins;
$data = $req->request->all();
$pins->setTitle($data['title']);
$pins->setDescription($data['description']);
$em->persist($pins);
$em->flush();
}
return $this->render('pins/create.html.twig');
}
Template :
{% extends 'layout/base.html.twig' %}
{% block title %}
{% endblock %}
{% block body %}
<form method="POST">
<div>
<label for="title">Titre</label><br>
<input type="text" id="title" placeholder="Titre" name="title">
</div>
<div>
<label for="description">Description</label><br>
<textarea id="description" rows="10" cols="100" placeholder="Ecrivez une description" name="description"></textarea>
</div>
<input type="submit" value="Valider">
</form>
{% endblock %}
routes.yaml
# index:
# path: /
# controller: App\Controller\QuestionController::homepage
routing.yaml
framework:
router:
utf8: true
# Configure how to generate URLs in non-HTTP contexts, such as CLI commands.
# See https://symfony.com/doc/current/routing.html#generating-urls-in-commands
#default_uri: http://localhost
Can anyone explain me why it's doesn't work with POST only ?