SlimFramework: Method not allowed. Must be one of: GET
I am having difficulty solving an error that is giving in slim framework
. I have a contact page that is accessed /p / contact, after accessing and clicking the Submit button, it tries to send the <form>
via POST to action = "" only it returns me:
Method not allowed.
Method not allowed. Must be one of: GET
I believe it's because he's thinking it'S GET and I'm sending the form via post.
The configured route that 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 take the data entered in the form via post to the url / p / contact
2 answers
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 to the method GET
, so the error of not allowing the method POST
. For the same route to 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 function map
defines the list of supported methods.
In the function body, you can get the method of the current request through:
$method = $req->getMethod();
Or check directly through from:
if ($req->isPost()) { ... }
I believe the values coming from the form will be accessible at $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');
If none of the above solutions work, make sure the Save button is inside the tag, if it is inside Test taking out 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>
In this way Run The Post via Ajax after clicking on the id save