Slimframework: Method not allowed. Must be one of: GET

Asked

Viewed 1,849 times

1

I’m having a hard time solving a mistake that’s been slim framework. I have a contact page that is accessed /p/contact, after accessing and clicking the send button, it tries to send the <form> via POST to the action="" only it returns me:

Method not allowed.

Method not allowed. Must be one of: GET

I guess it’s because he’s thinking it’s GET and I’m sending the form via post.

The route configures, which is calling the view is :

   $this->get('/p/{pag_slug}', function($req, $res, $args) {
        $Paginas = (new \App\Models\Pagina);
        $paginas = $Paginas::orderBy('ordem', 'ASC')->get();

        $Informacao = (new \App\Models\Informacao);
        $info = $Informacao::first();

        if ($args['pag_slug'] == 'contato') {
            return $this->view->render($res, 'site/contato.twig', [
                'paginas' => $paginas,
                'info'    => $info,
            ]);
        }
        
        $pagina = $Paginas::where('slug', $args['pag_slug'])->first();
        if ($pagina) {
            return $this->view->render($res, 'site/pagina.twig', [
                'paginas' => $paginas,
                'pagina'  => $pagina,
                'info'    => $info,
            ]);
        }
        
    })->setName('site.pagina');

What I need is to get the data entered in the form via post to the url /p/contact

2 answers

2


In the Slim framework, when setting the route as:

$this->get('/p/{pag_slug}', function($req, $res, $args) {
   // ...
})->setName('site.pagina');

You will be mapping the URL /p/{pag_slug} only for the method GET, therefore the error of not being allowed the method POST. For a single route accept multiple methods, you can do:

$this->map(['get', 'post'], '/p/{pag_slug}', function($req, $res, $args) {
   // ...
})->setName('site.pagina');

Where the first function parameter map sets the list of supported methods.

In the body of the function, you can obtain the current request method through:

$method = $req->getMethod();

Or check directly through:

if ($req->isPost()) { ... }

I believe the values coming from the form will be accessible on $req->post({name}).

Your code should look like:

$this->map(['get', 'post'], '/p/{pag_slug}', function($req, $res, $args) {
    $Paginas = (new \App\Models\Pagina);
    $paginas = $Paginas::orderBy('ordem', 'ASC')->get();

    $Informacao = (new \App\Models\Informacao);
    $info = $Informacao::first();

    if ($args['pag_slug'] == 'contato') {

        if ($req->isPost())
        {
            $name = $req->post("name");
            $email = $req->post("email");
            // Envia o e-mail...
        }

        return $this->view->render($res, 'site/contato.twig', [
            'paginas' => $paginas,
            'info'    => $info,
        ]);
    }

    $pagina = $Paginas::where('slug', $args['pag_slug'])->first();
    if ($pagina) {
        return $this->view->render($res, 'site/pagina.twig', [
            'paginas' => $paginas,
            'pagina'  => $pagina,
            'info'    => $info,
        ]);
    }

})->setName('site.pagina');
  • Friend worked rsrs. Thanks. Just not working $req->post() ... If I leave him, he’s error 500. I traded for $form = $req->getParsedBody();

  • That’s right, the method post existed until version 2. In version 3 you can use getParsedBody or getParam. For example: $name = $req->getParam("name");

0

In case none of the above solutions work, check if the save button is inside the tag, if it is inside the test by taking from inside the tag:

Example Wrong:

<form>
    <button class="btn btn-danger " id="salvar"> SALVAR </button>
 </form>

Correct:

 <form>

 </form>
<button class="btn btn-success " id="salvar"> SALVAR </button>

This way run the POST via Ajax after clicking the saved id

Browser other questions tagged

You are not signed in. Login or sign up in order to post.