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

1

I'm having trouble resolving an error that is occurring in slim framework . I have a contact page that is accessed / p / contact , after accessing and clicking the submit button, it tries to send <form> via POST to the action "" only it returns me :

  

Method not allowed.

     

Method not allowed. Must be one of: GET

I think 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 typed in the form via post to the url / p / contact

    
asked by anonymous 07.03.2017 / 23:42

1 answer

2

In the Slim framework, 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 to the GET method, hence the error that the POST method is not allowed. For the same route, accept multiple methods, you can do:

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

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

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

$method = $req->getMethod();

Or check directly through:

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

I believe that the values coming from the form will be accessible in $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');
    
07.03.2017 / 23:54