Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException。原因是啥?

Posted

技术标签:

【中文标题】Symfony\\Component\\HttpKernel\\Exception\\MethodNotAllowedHttpException。原因是啥?【英文标题】:Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException. What is the cause?Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException。原因是什么? 【发布时间】:2018-07-07 00:58:57 【问题描述】:

我对 Laravel 很陌生,但我遇到了一个模糊的错误。每当我尝试使用用户名和密码登录时,都会收到此错误。

Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException

我的代码是这样的:

用户控制器.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\User;

class UserController extends Controller

    public function postSignUp(Request $request)
        $firstName = $request['firstName'];
        $lastName = $request['lastName'];
        $username = $request['username'];
        $password = bcrypt($request['password']);
        $email = $request['email'];

        $user = new User();
        $user->first_name = $firstName;
        $user->last_name = $lastName;
        $user->username = $username;
        $user->password = $password;
        $user->email = $email;

        $user->save();

        Auth::login($user);

        return redirect()->back();
    

    public function postSignIn(Request $request)
        $username = $request['username'];
        $password = $request['password'];
        if (Auth::attempt(['username' => $username, 'password' => $password]))
            return redirect()->back();
        
    

提供者称为 User.php

<?php

namespace App;

use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;

class User extends Model implements Authenticatable

    use \Illuminate\Auth\Authenticatable;

路由文件web.php

<?php

Route::get('/', 'PagesController@index')->name('home');
Route::post('signup', 'UserController@postSignUp')->name('signup');
Route::get('signin', 'UserController@postSignin')->name('signin');

【问题讨论】:

Laravel 5.5 error when try to login的可能重复 【参考方案1】:

因为路由不存在。添加登录后路由。 postSignin 应该是一个 post 路由

Route::post('signin', 'UserController@postSignin')->name('signin');

Route::get('signin', 'UserController@getSignin')->name('signInForm');

代替

Route::get('signin', 'UserController@postSignin')->name('signin’);

【讨论】:

【参考方案2】:

MethodNotAllowed 表示您正在使用网络服务器不喜欢该请求的 VERB...即 GET 而不是 POST。

您的登录控制器名为 postSignIn,但我注意到您使用 get 调用它

【讨论】:

以上是关于Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException。原因是啥?的主要内容,如果未能解决你的问题,请参考以下文章