-1

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)

enter image description here

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 ?

DarkBee
  • 16,592
  • 6
  • 46
  • 58
Giyash
  • 11
  • 1
  • 2
  • Can you show your route declaration? – Wesley Smith Jul 18 '20 at 21:23
  • url in my navigator or routes.yaml ? or another file ? because except the controller and template i don't touch anything. – Giyash Jul 18 '20 at 21:46
  • yeah, wherever the route is defined in your application. Interested to see how its defined exactly. – Wesley Smith Jul 18 '20 at 21:49
  • Just as a side thought, there's not a redirect happening in there anywhere, is there? POST requests can be redirected and turn into GET requests. That's bitten me before. – Wesley Smith Jul 18 '20 at 21:52
  • no no redirect in GET, i don't know how to do that yet ^^ the form is supposed to be redirected to the same page – Giyash Jul 18 '20 at 22:01
  • I see, reviewed their docs, symfony allows declaring the route via annotations. I see yours now. Just to be clear, you're actually submitting this form right? Not catching the submission and sending via ajax or anything like that right? – Wesley Smith Jul 18 '20 at 22:23
  • Hmm, I just noticed that the route declaration looks odd `@Route("/pins/create",name="blog_show", methods={"POST"})` specifically, the `name="blog_show"`. is that left over from copying a different bit of code? If so, what happens if you change that name? Longshot, but curious. – Wesley Smith Jul 18 '20 at 22:42
  • yeah it's i change it by create_pin – Giyash Jul 19 '20 at 07:19
  • Does it work after that? – Wesley Smith Jul 19 '20 at 07:20
  • Same error... :( The problem may come from my PHP version ? I have **PHP 7.4.8 (cli) (built: Jul 7 2020 12:15:54) ( ZTS Visual C++ 2017 x64 )** – Giyash Jul 19 '20 at 07:32
  • Unlikely, lemme ask this, do you have another method somewhere with something like `@Route("/pins/create",name="create_pin", methods={"GET"})` for displaying this page? – Wesley Smith Jul 20 '20 at 13:22

1 Answers1

0

Okay, so usually, a website is visited via GET request, to display the form - which probably already causes the error you get.

Then you submit the form, which triggers a POST request, which your controller would be able to handle.

The answer is to remove the methods attribute from @Route annotation entirely or add GET to it (methods={"POST","GET"}).

The ugly alternative would be to have a different page with that form and send it to the route you got. but that would be silly.

Jakumi
  • 8,043
  • 2
  • 15
  • 32
  • If this were the case, the OP would get the error when displaying the form, not when submitting it – Wesley Smith Jul 20 '20 at 13:14
  • @WesleySmith but the error message clearly says `"GET /pins/create": Method Not Allowed (Allow: POST)`, which means it's GET, and since the form is POST, it's when displaying the form. ;o) so yes, I believe the error occurs when displaying the form and OP did word the question in an unfortunate way. – Jakumi Jul 20 '20 at 13:16
  • Hmm, possibly, but when the OP says "I want to get only the method POST but when it's working only if I add GET too", that makes me think otherwise because adding the GET to the shown route alone would not fix this because there's no logic in that function to display the page with the form. Because of that, it seems the GET route that displays the form must already be defined elsewhere. – Wesley Smith Jul 20 '20 at 13:26
  • 1
    @WesleySmith don't get me wrong, I totally get your point. My first assumption (maybe false) is that OP is a beginner. The controller function clearly renders a template with the name `create` in it, which leads me to believe it's the very template OP shows before. technically you absolutely can be right ... I just have different assumptions. But OP didn't mention my answer to not work and possibly only wanted an explanation. ;o) – Jakumi Jul 20 '20 at 13:31
  • Thats fair, I could be giving the OP too much credit. if you edit the answer (so SO will let me) ill remove my hastily applied downvote till we know more :) – Wesley Smith Jul 20 '20 at 13:34