Silex Framework, no route found for POST - php

I have a little problem with the silex framework (I'm quite sure, that it's caused by Silex)..
I have a form and want to submit it with POST, but Silex throws the following exceptions:
MethodNotAllowedException in UrlMatcher.php line 101:
MethodNotAllowedHttpException in RouterListener.php line 149:
No route found for "POST /checkPW": Method Not Allowed (Allow: GET)
Thats what my controller looks like:
$app->get('/checkPW', function () use ($app) {
return $app['templating']->render(
'checkPW_blog.php'
);
});
And that's what the form looks like:
<form method="post" action="/checkPW">
<div class="modal-body">
<div class="form-group">
<input type="password" class="form-control" id="password"
name="password"
placeholder="Passwort">
</div>
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-success" id="submitPW">
Passwort bestätigen
</button>
</div>
</form>
(realised with Bootstrap)
The bizarre thing is, that when I send the form with the method GET instead of POST everything works fine...
Does anybody know whats the problem here..?
Thanks all.

Look, you only define a route for get:
$app->get('/checkPW', function () use ($app) {
return $app['templating']->render(
'checkPW_blog.php'
);
});
Just define one for post:
$app->post('/checkPW', function () use ($app) {
// do post stuff...
});

Related

error: The POST method is not supported for this route. Supported methods: GET, HEAD. - using laravel livewire

I'm trying to do an image upload using Laravel livewire, but when I click on the button "upload" to test the functionality this error appears
The POST method is not supported for this route. Supported methods: GET, HEAD'
The programs:
ROUTE
Route::get('/upload', UploadFoto::class)->name('upload.foto.user');
CONTROLLER (using dd for the tests)
<?php
namespace App\Http\Livewire\User;
use Livewire\Component;
class UploadFoto extends Component
{
public $foto;
public function render()
{
return view('livewire.user.upload-foto');
}
public function storageFoto()
{
dd('aqui');
}
}
VIEW
#extends('layouts.app')
#section('content')
<div>
{{-- To attain knowledge, add things every day; To attain wisdom, subtract things every day. --}}
<form action="#" method="post">
<input type="file" name="foto" id="foto" wire:submit.prevent="storageFoto">
<button type="submit">Upload</button>
</form>
</div>
#endsection
You set get method on this route - but upload use post method. Change it:
Route::post('/upload', UploadFoto::class)->name('upload.foto.user');
please check this, you added submit in wrong place of form
<div>
<form wire:submit.prevent="storageFoto" method="post">
<input type="file" name="foto" id="foto">
<button type="submit">Upload</button>
</form>
</div>
and you should check this lines into app file
#livewireStyles
#livewireScripts
change route method to Post
Route::post('/upload', UploadFoto::class)->name('upload.foto.user');

How to fix Update error for The POST method is not supported for this route in Laravel 7

I'm new to laravel I'm trying to create an Todo App for a simple CRUD project but I got an Error that said
The POST method is not supported for this route. Supported methods: GET, HEAD, PUT, PATCH, DELETE.
I've been searching for the solution and try them all but somehow the solution does not fix my error.
web.php
Route::get('/', 'TaskController#index');
Route::resource('task','TaskController');
TaskController
public function edit(Task $task)
{
return view('task.edit')->with('task', $task);
}
public function update(Request $request, Task $task)
{
$task->update($request->all());
return redirect('/');
}
edit.blade.php
<form action="{{route('task.update', $task->id)}}" method="POST">
#csrf
method('PATCH')
<div class="input-group mb-3 w-100">
<input type="text" class="form-control form-control-lg" name="title" value="{{$task->title}}" aria-label="Recipient's username" aria-describedby="button-addon2">
<div class="input-group-append">
<button class="btn btn-success" type="submit" id="button-addon2">Update</button>
<div>
<div>
</form>
Route List:
Update operations is traditionally done with PUT or PATCH calls. Form does not support this, but Laravel supports doing an hidden input field with the name _method, that indicate if it should be a PUT.
Your syntax is not correct and should be.
<form action="{{route('task.update', $task->id)}}" method="POST">
#csrf
#method('PATCH')

Laravel form include input in action attribute

LARAVEL 5.0
PHP 5.4.45
I have a route that is shaped like this :
/app/Http/routes.php
Route::get('/clients/search/{id}', 'ClientController#searchById')->where('id', '[0-9]+');
Route::get('/clients/search/{text}', 'ClientController#searchByText')->where('text', '[a-zA-Z]');
I will not print my view here but it simply search for the exact client (case id) or the first 10 clients (case text).
Then I want to create a search form. I created the route :
/app/Http/routes.php
// Route::get('/clients/search/{id}', 'ClientController#searchById')->where('id', '[0-9]+');
// Route::get('/clients/search/{text}', 'ClientController#searchByText')->where('text', '[a-zA-Z]');
Route::get( '/clients/search', 'ClientController#search');
The controller for this route :
/app/Http/Controllers/ClientController.php
<?php
namespace App\Http\Controllers;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
class ClientController extends Controller {
// Controllers for the 'searchById' and 'searchByText'
public function search() {
return view('client.search.search', [
'title' => 'Client search form',
'title_sub' => ''
]);
}
}
And the view for this search form :
/ressources/view/client/search/search.blade.php
<form action="/clients/search/INPUT HERE ?" method="get">
<div class="input-group">
<input id="newpassword" class="form-control" type="text" name="password" placeholder="Id de client, nom, prénom, ...">
<span class="input-group-btn">
<button id="button_search" class="btn green-haze" type="submit"><i class="fa fa-search fa-fw"></i></button>
</span>
</div>
</form>
QUESTION
How can I, before submit, pass an input as a part of my action attribute for my form ? The point is to be able to launch those kind of requests :
/clients/search/26
/clients/search/Mike%20%Folley
/clients/search/Paris
So my controllers handling this route could do the job. Is there any way to do that ? Or should I go for JavaScript solution (which make me sad a bit) ?
Yes, you need javascript to modify request URL before it is even sent :). No form tag needed for that, vanilla js approach like this might be sufficient:
<input type="text" id="search">
<button onclick="search()">
Submit
</button>
<script>
function search() {
window.location='/search/' +
encodeURIComponent(document.getElementById('search').value);
}
</script>
You can and should just use one route and then in controller's method decide on what to search and what view to return. Routes should contain the least amount of logic possible.

Laravel- Form post going to wrong URL

I am learning Laravel from scratch.
I am doing simple form "POST" but something is missing and I am not able to find out what is missing.
So here is my "routes.php":
Route::get('cards','CardsController#all');
Route::get('cards/{card}','CardsController#show');
Route::post('cards/{card}/notes','NotesController#new');
and view:
<div>
<form method="POST" action="cards/{{ $card->id }}/notes">
<div class="form-group">
<label>Note body:</label>
<textarea name="body" class="form-control"></textarea>
</div>
<button type="submit" class="btn btn-primary">Add</button>
</form>
</div>
and here is controller:
`
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
class NotesController extends Controller
{
public function new(Request $request){
return $request->all();
}
}
`
Now when I submit the form it gives me following error:
NotFoundHttpException in RouteCollection.php line 161
and the URL in browser becomes:
http://localhost:88/learning/cards/cards/1/notes
which is definitely wrong.
I think I am missing something very basic.
Please guide me.
Thank you.
Change the form action to,
<form method="POST" action="{{ url('cards/'.$card->id.'/notes') }}" >
Your action starts with "cards/". Change the action to start with '/cards/' so that it's path is not calculated relative to the current route.
<form method="POST" action="/cards/{{ $card->id }}/notes">

Laravel 4 URL::action error

I have a controller called CharactersController.php in my controllers directory. Here are the two functions:
public function search()
{
return View::make('search.search');
}
public function post_search()
{
$name = Input::get('character');
$searchResult = Player::where('name', 'LIKE', '%'.$name.'%')->paginate(5);
return View::make('search.post_search')
->with('name', $name)
->with('searchResult', $searchResult);
}
In the first function (function search()) I return a view. Here's the code of the view(just the form):
<form id="custom-search-form" class="form-search form-horizontal pull-right" action="{{ URL::action('CharactersController#post_search') }}" method="get">
<div class="input-append spancustom">
<input type="text" class="search-query" name="character" placeholder="Character/guild name">
<button type="submit" class="btn"><i class="icon-search"></i></button>
</div>
</form>
When I try to run the form (to search) I get an Unknown action [CharactersController#post_search]. error. I had this error before, I tried switching controllers, tried doing everything. But it didn't work. So I gave up.
Anyone who can solve it?
Is that controller RESTful?
And Have you created a Route::controller() in the routes.php?
If it's not RESTFul, can you try removing post_ in the method's title?
Have you actually defined a route for the controller's method? You need to do that, else that pesky exception will be thrown when you call URL::action()
Function names need to be camelCased per PSR-1. Chnage post_search to postSearch

Categories