I will get MethodNotAllowedHttpException when submitting a form in laravel
HTML file
<form action="{{ action('HomeController#store') }}" method="post">
<input name="_method" type="hidden" value="PATCH">
<input name="_token" type="hidden" value="{{ csrf_token() }}"/>
<input type="submit" name="Submit" value="submit">
</form>
Im my routes.php
Route::post('formaction','HomeController#store')
Controller.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class HomeController extends Controller
{
public function store(){
echo 'form submitted';
}
}
Why i will get MethodNotAllowedHttpException in my form action page?
I've refereed some questions related to this,but nothing help me
Even if the form is using the POST method, you are sending the extra param _method which will let the framework know that you want to use that method instead. If you send that extra param then you should set the route accordingly:
Route::patch('formaction','HomeController#store');
Related
I create a CRUD application in Laravel 7 and MongoDB. Adding items to the collection works fine. But I wanted to delete an item and unfortunately Laravel stopped liking me... I get an error:
Action App\Http\Controllers\InQuestController#destroy not defined.
I checked a dozen times and wrote such a function
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Quests;
use DB;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
class InQuestsController extends Controller
{
...
public function destroy($id) {
$quest = Quests::find($id);
$quest->delete();
return redirect('inside');
}
}
To function I am reffering from:
<form action="{{ action('InQuestsController#destroy', $quest->id) }}" method="POST">
#csrf
<input name="_method" type="hidden" value="DELETE">
<button id="details" class="idButton detailsButton" type="button"><img src="{{ asset('images/arrow.png') }}" alt="" class="icon arrow"></button>
<button class="editButton" type="button"><img src="{{ asset('images/pencil.svg') }}" alt=""></button>
<button class="deleteButton" type="submit"> <img src="{{ asset('images/bin.png') }}" alt=""></button>
</form>
What's wrong?
FIX:
In web.php add line:
Route::delete('{id}', 'InQuestsController#destroy')->name('destroy');
This is my form:
<form class="form-horizontal" method="POST" action="{{ url('/categories/new') }}">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input class="btn btn-default" value="Cancel" type="reset">
</form>
This is my url where my form is located: /categories/new
This is my route:
Route::get('/categories/new', 'Admin\CategoriesController#newCategory');
I want to keep the new method, so i want to check if there is a post method do smth else load the view with my form. How can I achieve this in laravel 5. I'm a newbie so all of the detailed explanations are welcomed. Thank you !
If you want to use single method for both POST and GET requests, you can use match or any, for example:
Route::match(['get', 'post'], '/', 'someController#someMethod');
To detect what request is used:
$method = $request->method();
if ($request->isMethod('post')) {
https://laravel.com/docs/master/requests#request-path-and-method
Add this to your rotes file:
Route::post('/categories/new', 'Admin\CategoriesController#someOtherFunctionHere');
Im trying to POST data to my controller but I'm getting a
MethodNotAllowedHttpException in RouteCollection.php line 219:
error message, here are my files.
my route file
<?php
Route::get('/', function () {
return view('welcome');
});
// Authentication routes
Route::get('auth/login', 'Auth\AuthController#getLogin');
Route::post('auth/login', 'Auth\AuthController#postLogin');
Route::get('auth/logout', 'Auth\AuthController#getLogout');
// Registration routes
Route::get('register', 'Auth\AuthController#getRegister');
Route::post('auth/register', 'Auth\AuthController#postRegister');
Route::controllers(['password' => 'Auth\PasswordController',]);
Route::get('/home', 'HomeController#index');
// Using A Route Closure
Route::get('profile', ['middleware' => 'auth', function() {
// Only authenticated users may enter...
Route::auth();
}]);
// practicing using forms for sending data to the DB & populating form fields with DB data
Route::get('profile', 'ProfileController#index');
Route::post('profile/update', 'ProfileController#updateProfile');
profile.blade.php
<form method="POST" action="/profile/update/">
<div class="form-group hidden">
<input type="hidden" name="id" value="<?php echo $users[0]->id;?>">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input name="_method" type="hidden" value="PATCH">
</div>
<div class="form-group">
<label for="email"><b>Name:</b></label>
<input type="text" name="name" placeholder="Please enter your email here" class="form-control"/>
</div>
<div class="form-group">
<label for="email"><b>Email:</b></label>
<input type="text" name="email" placeholder="Please enter your email here" class="form-control"/>
</div>
<div class="form-group">
<button type="submit" class="btn btn-default"> Submit </button>
</div>
</form>
& my ProfileController.php
<?php
namespace App\Http\Controllers;
use Auth;
use App\User;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class ProfileController extends Controller
{
/**
* Update user profile & make backend push to DB
**/
public function index()
{
if(Auth::check()) {
// connecting to the DB and accessing
$users = User::all();
//var_dump($users);
return view('profile', compact('users'));
}
return view('auth/login');
}
public function updateProfile(Requests $request) {
return $request->all();
}
}
not sure what the issue is. Thanks for all the help everyone
A couple of issues that we managed to address here:
Over-use of HTTP Verbs
At your view, you have: <form method="POST" but also <input name="_method" type="hidden" value="PATCH"> which may conflict between a POST and a PATCH. Since your routes.php only declares POST, let's remove the patch definition.
Routing Mistake
Still at your view, your action points to action="/profile/update/" while your route is defined as Route::post('profile/update'), notice the extra / at the end in your form. That slash should not be there.
Controllers Request
You have a here: use App\Http\Requests; is probably incorrect because that's a folder within Laravel, not a class. Let's remove that and keep use Illuminate\Http\Request; for now. In the near future, you'll be learning how to create your own Form Requests and you'll probably want a UpdateProfileRequest.php.
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">
I am using a resource Laravel route defined by the following line in my routes.php :
Route::resource('test', 'App\Controllers\Teacher\TestController', ['only' => ['index', 'create', 'destroy']]);
The index method works fine. In the template of index I have created a form in order to remove an item of the list.
<form method="DELETE" action="{{ URL::action('App\Controllers\Teacher\TestController#destroy', $audit->id ) }}">
<input type="submit" value="Remove" />
</form>
The URL is correctly generated by Laravel but when I post this form I get the following error :
exception 'Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException' in /var/www/project/bootstrap/compiled.php:5365
I have already try to change DELETE by POST in the method attribute of my form but it doesn't work.
I also read this post but it doesn't help me : MethodNotAllowedHttpException on resource defined method Laravel-4
When you manually create your form you should use POST as method, and use _method input with delete value this way:
<form method="POST" action="{{ URL::action('App\Controllers\Teacher\TestController#destroy', $audit->id ) }}">
<input type="hidden" name="_method" value="DELETE" />
<input type="submit" value="Remove" />
</form>
Reference in Laravel documentation for form method spoofing
Try this:
<form action="/test" ....>