I will get MethodNotAllowedHttpException when submitting a form in laravel
Html file
<form method="POST" action="/cards/{{$card->id}}/notes">
<input name="_token" type="hidden" value="{{ csrf_token() }}"/>
<textarea name="body" class="form-control"></textarea>
<button type="submit">Add Note</button>
</form>
routes.php
Route::post('cards/{card}/notes','NotesController#store');
NotesController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
class NotesController extends Controller
{
public function store()
{
return request()->all();
}
}
Make sure you don't have a route, say a Route::post with a parameter that lies in front of the route you are trying to hit.
For example:
Route::post('{something}', 'SomethingController#index');
Route::post('cards/{card}/notes', 'NotesController#store');
In this case, no matter what you try to send to the cards route, it will always hit the something route because {something} is intercepting cards as a valid parameter and triggers the SomethingController.
Put the something route below the cards route and it should work.
MethodNotAllowedHttpException is thrown when no matching route (method and URI) was found, but a route with a matching URI but not matching method was found.
In your case, I guess the issue is because URI parameters differ between the route and the controller.
Here are two alternatives you can try:
Remove the parameter from your route:
Route::post('cards/notes','NotesController#store');
Add the parameter to your controller:
public function store($card)
{
return request()->all();
}
I have tried to solve this error in lumen and it took me quite a lot of time to figure out the problem.
The problem is with laravel itself.
Sometimes if you have another route like GET device/{variable}, laravel stops in this first route...
So what you need to do is change the route POST device to POST device/add
This link helped me a lot
Related
I want to access my controller and show and log the test messages.
I have been trying to access my controller via a route but it doesn't seem to work.
It's my first time using Laravel so I hope anyone can help I have been struggling with this problem for a couple of hours.
It hasn't been showing error messages, but I can't find any mistakes in my syntax I tried finding if there might be possible problems in my php.ini files, but I don't know.
the form
<form method="post" action="{{ Route('saveItem') }}" accept-charset="UTF-8">
{{ csrf_field() }}
<label for="listItem">New Todo Item</label><br>
<input type="text" name="listItem">
<button type="submit">Save item</button>
</form>
Route
Route::post('/saveItemRoute', [TodoListController::class, 'saveItem'])->name('saveItem');
Controller
class TodoListController extends Controller
{
public function saveItem(Request $request) {
echo("<h1>victory2</h1>");
\Log::debug("TodoListController");
\Log::info(json_encode($request->all()));
$newListItem = new ListItem;
$newListItem->name = $request->ListItem;
$newListItem->is_complete = 0;
$newListItem->save();
return view('welcome');
}
}
After trying again it seem like the rout doesnt function / do anything and i cant figure out why
I know what whent wrong after changes to routing i needed to do this line in the cmd
php artisan route:clear
the first time i tested the routing code but i did not know after changes you need to clear the route
I'm trying to create a simple CRUD application with Laravel 9. I've run into a problem with HTML forms. I've created a page where you can edit data on rabbits existing in my database.
My form
<form name="editRabbitForm" action="{{ url('/rabbit/update') }}" method="PUT">
{{ csrf_field() }}
<!-- here be input fields -->
<button type="submit" class="btn btn-success">Save</button>
<a type="button" href="/rabbits" class="btn btn-danger">Cancel</a>
</form>
web.php routes
<?php
use Illuminate\Support\Facades\Auth;
use App\Http\Controllers\QuoteController;
use App\Http\Controllers\RabbitController;
use Illuminate\Support\Facades\Route;
Route::get('/rabbits', [RabbitController::class, 'index']);
Route::get('/rabbit/edit/{id}', [RabbitController::class, 'edit']);
Route::put('/rabbit/update', [RabbitController::class, 'update']);
RabbitController.php
<?php
namespace App\Http\Controllers;
use App\Models\Rabbit;
use Illuminate\Http\Request;
class RabbitController extends Controller
{
public function index() {
return view('rabbits.index', ['rabbits' => Rabbit::all()]);
}
public function edit($id) {
return view('rabbits.edit', ['rabbit' => Rabbit::where('breed_id', $id)->first()]);
}
public function update(Request $request) {
echo("SOME SORT OF RESULT!");
var_dump($request);
}
}
Before I even hit the controller I get an exception reading:
The GET method is not supported for this route. Supported methods: PUT.
I really don't get what I'm doing wrong in this scenario
As stated above in my comment:
To send a put request you will need to change the method to POST and add #method('PUT') in your form. This will insert a hidden input for the method. Laravel will then automatically route this request to the method specified by the put route in your routes file.
This is needed because as #ADyson writes, browsers are limited to GET and POST request.
And last but not least, browsers or in this case HTML forms are stupid.
Maybe someday this will be changed, who knows.
I am writting APIs for android/ios using Laravel 5.4. My simple webservice signUp which is working perfectly on localhost not working on live server and giving
MethodNotAllowedHttpException on POST methods
GET call works perfect .
Route code
Route::post('/signUp',['uses'=>'API_UserController#userSignUp']);
Attached is screen shot link of my postman.
https://i.stack.imgur.com/060IF.png
here is quick example of flow try this
<form action="{{url('v1/userSignUp')}}" class="validation" method="post"
accept-charset="utf-8">
{{ csrf_field() }}
<div>
// your required form field
<input type="submit" value="Add Category" class="btn btn-primary" />
</div>
</form>
and in your route add this
Route::get('/signUp', function () {
return view('yourviewpagename');
});
Route::post('/signUp','API_UserController#userSignUp');
and in your API_UserController
public function userSignUp()
{
dd(request->all());
}
#Mujeeb Ur Rehman Post method is use to send data and get method is use to access data in view or just to render view.
eg:-route:post('/home',xxController#xx);
route:get('/home1',xxController#xx);
error which you are getting is because suppose if you url it like this localhost/xx/home your framework think to access data or post data framework get confuse in this
just use like this
Route::group(['prefix' => 'v1'], function () {
Route::post('/signUp','API_UserController#userSignUp');
});
when you hit via postman tour URL is http://your_domain/api/v1/signUp so just add a prefix it will automatically add with your URL. in route group u can also use many other things like namespace, middleware etc. Example :
Route::group(['namespace' => 'any_depend_on_your_project_structure', 'middleware' => 'any_middleware', 'prefix' => 'v1'], function() {
// your routes
});
I just wondering in my project. I have a form that can be access at localhost/app/esetting/mymail and this is the code in the view:
....
<form action="{{ url( 'app/esetting/emailautomsave' ) }}" class="form-horizontal" method="post" enctype="multipart/form-data">
{{ csrf_field() }}
...
<input type="submit" value="save">
</form>
but when I try to click for form submission, I expect it to go to app/esetting/emailautomsave and calls it controller which is on my SettingsController.php.
public function postEmailautomsave(Request $request){
...
}
but it redirects to localhost/app/mymail? and give me this error:
NotFoundHttpException in Controller.php line 93:
Controller method not found.
this sounds weird on my end. can anyone have an idea about this? I am sure I have done the right this specially on my routes.php
Route::group([ 'prefix' => 'app', 'middleware' => 'auth' ], function() {
....
Route::controller('esetting', 'SettingController');
Route::get( 'esetting/mymail', 'SettingController#viewEmailAutom' ); // view for the form to display
....
Just simply define:
Route::post('esetting/mymail/emailautomsave', 'SettingController#postEmailautomsave');
I know that You'll say: "I've defined Route::controller, so it will look for it atuomatically."
But for me is best to have routes defined exactly.
also if:
it redirects to localhost/app/mymail? and give me this error:
NotFoundHttpException in Controller.php line 93: Controller method
not found.
maybe it means that some middleware is redirecting You there?
You can check it by simply doing this:
public function postEmailautomsave(Request $request){
die('test');
...
}
if it will redirect so it mean that some function was called before and redirect browser to app/mymail.
If you use Route::controller you need to define you methods according to the route.
E.g.
Route::controller('esetting', 'SettingController');
Will look for SettingController#getIndex when you visit http:://yoursite.com/esetting
To reach SettingController#postEmailAutomsave() you would need to go to the route http:://yoursite.com/esetting/emmail/automsave
I have not used Route::controller myself, I got used too (and now prefer) naming them individually.
But this should do the trick.
Laravel 5.1 docs
In a controller name tagController where I define a custom method
public function addTag($id)
{
$book = $id;
return view('tag.create', compact('book'));
}
In the route I define the custom route method
Route::get('tag/addTag/{$id}', 'tagController#addTag');
Route::resource('tag', 'tagController');
From my view I'm calling the controller method
<a class="btn btn-primary various" href="{{url('/tag/addTag', $tag->id)}}">Add Tag</a>
I'm getting the error every time
NotFoundHttpException in RouteCollection.php line 143:
This is routing problem but I don't understand to how to define a custom method in route and in resourceful controller. Please help to get rid of the error?
Thanks.
Remove the $ from the path
Route::get('tag/addTag/{id}', 'tagController#addTag');