幼鱼抛掷方法

我在试着让一些非常基本的东西运转起来。我已经习惯了 CI,现在正在学习 Laravel 4,他们的文档并没有让它变得容易!无论如何,我正在尝试创建一个登录表单,通过在下一个表单中打印它来确保数据被成功发布。我得到了一个例外:

Symfony 组件 HttpKernel 异常 MethodNotAllowedHttpException

以及我的会员控制器:

    public function index()
{
if (Session::has('userToken'))
{
/*Retrieve data of user from DB using token & Load view*/
return View::make('members/profile');
}else{
return View::make('members/login');
}
}


public function validateCredentials()
{
if(Input::post())
{
$email = Input::post('email');
$password = Input::post('password');
return "Email: " . $email . " and Password: " . $password;
}else{
return View::make('members/login');
}
}

路线包括:

Route::get('/', function()
{
return View::make('hello');
});


Route::get('/members', 'MemberController@index');
Route::get('/validate', 'MemberController@validateCredentials');

最后,我的视图 login.php 有这样一个表单方向:

<?php echo Form::open(array('action' => 'MemberController@validateCredentials')); ?>

任何帮助都将不胜感激。

388365 次浏览

My suspicion is the problem lies in your route definition.

You defined the route as a GET request but the form is probably sending a POST request. Change your route definition to match the form's request method.

Route::post('/validate', [MemberController::class, 'validateCredentials']);

It's generally better practice to use named routes (helps to scale if the controller method/class changes).

Route::post('/validate', [MemberController::class, 'validateCredentials'])
->name('member.validateCredentials');

In the view, use the validation route as the form's action.

<form action="\{\{ route('member.validateCredentials') }}" method="POST">
@csrf
...
</form>
<?php echo Form::open(array('action' => 'MemberController@validateCredentials')); ?>

by default, Form::open() assumes a POST method.

you have GET in your routes. change it to POST in the corresponding route.

or if you want to use the GET method, then add the method param.

e.g.

Form::open(array('url' => 'foo/bar', 'method' => 'get'))

You are getting that error because you are posting to a GET route.

I would split your routing for validate into a separate GET and POST routes.

New Routes:

Route::post('validate', 'MemberController@validateCredentials');


Route::get('validate', function () {
return View::make('members/login');
});

Then your controller method could just be

public function validateCredentials()
{
$email = Input::post('email');
$password = Input::post('password');
return "Email: " . $email . " and Password: " . $password;
}

I encountered this problem as well and the other answers here were helpful, but I am using a Route::resource which takes care of GET, POST, and other requests.

In my case I left my route as is:

Route::resource('file', 'FilesController');

And simply modified my form to submit to the store function in my FilesController

\{\{ Form::open(array('route' => 'file.store')) }}

This fixed the issue, and I thought it was worth pointing out as a separate answer since various other answers suggest adding a new POST route. This is an option but it's not necessary.

I faced the error,
problem was FORM METHOD

\{\{ Form::open(array('url' => 'admin/doctor/edit/'.$doctor->doctor_id,'class'=>'form-horizontal form-bordered form-row-stripped','method' => 'PUT','files'=>true)) }}

It should be like this

\{\{ Form::open(array('url' => 'admin/doctor/edit/'.$doctor->doctor_id,'class'=>'form-horizontal form-bordered form-row-stripped','method' => 'POST','files'=>true)) }}

My problem was not that my routes were set up incorrectly, but that I was referencing the wrong Form method (which I had copied from a different form). I was doing...

{!! Form::model([ ... ]) !!}

(with no model specified). But I should have been using the regular open method...

{!! Form::open([ ... ]) !!}

Because the first parameter to model expect an actual model, it was not getting any of my options I was specifying. Hope this helps someone who knows their routes are correct, but something else is amiss.

I also had the same error but had a different fix, in my XYZ.blade.php I had:

{!! Form::open(array('ul' => 'services.store')) !!}

which gave me the error, - I still don't know why- but when I changed it to

{!! Form::open(array('route' => 'services.store')) !!}

It worked!

I thought it was worth sharing :)

Generally, there is a mistake in the HTTP verb used eg:

Calling PUT route with POST request

The problem is the you are using POST but actually you have to perform PATCH To fix this add

<input name="_method" type="hidden" value="PATCH">

Just after the Form::model line

well when i had these problem i faced 2 code errors

{!! Form::model(['method' => 'POST','route' => ['message.store']]) !!}

i corrected it by doing this

{!! Form::open(['method' => 'POST','route' => 'message.store']) !!}

so just to expatiate i changed the form model to open and also the route where wrongly placed in square braces.

In my case, I was sending a POST request over HTTP to a server where I had set up Nginx to redirect all requests to port 80 to port 443 where I was serving the app over HTTPS.

Making the request to the correct port directly fixed the problem. In my case, all I had to do is replace http:// in the request URL to https:// since I was using the default ports 80 and 443 respectively.

Laravel sometimes does not support {!! Form::open(['url' => 'posts/store']) !!} for security reasons. That's why the error has happened. You can solve this error by simply replacing the below code

{!! Form::open(array('route' => 'posts.store')) !!}




Error Code {!! Form::open(['url' => 'posts/store']) !!}

Correct Code {!! Form::open(array('route' => 'posts.store')) !!}

In my case, it was because my form was sending to a route with a different middleware. So it blocked from sending information to this specific route.

Typically MethodNotAllowedHttpException happens when

route method does not match.

Suppose you define POST request route file, but you sending GET request to the route.

That is because you are posting data through a get method.

Instead of

Route::get('/validate', 'MemberController@validateCredentials');

Try this

Route::post('/validate', 'MemberController@validateCredentials');
// not done
Route::post('`/posts/{id}`', 'PostsController@store')->name('posts.store');


return redirect('`/posts'`)->with('status','Post was created !');


// done
Route::post('`/posts`', 'PostsController@store')->name('posts.store');


return redirect('`/posts'`)->with('status','Post was created !');

As answered here by rebduvid you can use Route::match as below

Route::match(['get', 'post'], 'results',[
'as' => 'results_path',
'uses' => 'SearchController@results' ]);

update the parameters as per your logic

In my case, I was hitting an auth protected endpoint and my auth token was invalid. Getting a new token resolved the issue. This is a new older project I'm working on so there could be something in the implementation causing this.

In my case, it worked to check the https protocol, I in postman had defined a route with http, when the server was with https. I just corrected it and it worked for me