php Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException 无消息 Laravel 5.5

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48225095/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:38:18  来源:igfitidea点击:

Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException No message Laravel 5.5

phplaravellaravel-5.5

提问by

This is doing my head in. I'm getting this error when trying to login from a form:

这是我的头脑。尝试从表单登录时出现此错误:

Symfony \ Component \ HttpKernel \ Exception \

MethodNotAllowedHttpException

No message

Symfony \ 组件 \ HttpKernel \ 异常 \

MethodNotAllowedHttpException

没有消息

LoginController.php

登录控制器.php

<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class LoginController extends Controller
{
    use AuthenticatesUsers;
    protected $username = 'username';
    protected $redirectTo = '/dashboard';
    protected $guard = 'web';
    public function getLogin()
    {
        if (Auth::guard('web')->check())
        {
            return redirect()->route('dashboard');
        }
        return view('login');
    }
    public function postLogin(Request $request)
    {
        $auth = Auth::guard('web')->attempt(['username' => $request->username, 'password' => $request->password, 'active' => 1]);
        if ($auth)
        {
            return redirect()->route('dashboard');
        }
        return redirect()->route('/');
    }
    public function getLogout()
    {
        Auth::guard('web')->logout();
        return redirect()->route('/');
    }
}

The following route when i submit a form is working fine.

当我提交表单时,以下路线工作正常。

Route::get('/', ['as' => '/', 'uses' => 'LoginController@getLogin']);
Route::get('/login', ['as' => 'login', 'uses' => 'LoginController@getLogin']);

Route::group(['middleware' => ['autheticates', 'roles']], function (){
    Route::get('/logout', ['as' => 'logout', 'uses' => 'LoginController@getLogout']);
    Route::get('/dashboard', ['as' => 'dashboard', 'uses' => 'DashboardController@dashboard']);
});

Middleware/Autheticates.php

中间件/Autheticates.php

class Autheticates
{
    public function handle($request, Closure $next, $guard = 'web')
    {
        if (!Auth::guard($guard)->check())
        {
            return redirect()->route('/');
        }
        return $next($request);
    }
}

Middleware/Roles.php

中间件/角色.php

class Roles
{
    public function handle($request, Closure $next)
    {
        $roles = $this->getRequiredRoleForRoute($request->route());
        if ($request->user()->hasRole($roles) || $roles){
            return $next($request);
        }
        return redirect()->route('noPermissions');
    }
    private function getRequiredRoleForRoute($route)
    {
        $actions = $route->getAction();
        return isset($actions['roles']) ? $actions['roles'] : null;
    }
}

login.blade.php

登录名.blade.php

<form class="login-form" action="{{ route('login') }}" method="post">
    {{ csrf_field() }}
    <div class="login-wrap">
        <p class="login-img"><i class="icon_lock_alt"></i></p>
        <div class="input-group">
            <span class="input-group-addon"><i class="icon_profile"></i></span>
            <input type="text" name="username" class="form-control" placeholder="Username" autofocus>
        </div>
        <div class="input-group">
            <span class="input-group-addon"><i class="icon_key_alt"></i></span>
            <input type="password" name="password" class="form-control" placeholder="Password">
        </div>
        <label class="checkbox">
            <input type="checkbox" value="remember-me"> Remember me
            <span class="pull-right"> <a href="#"> Forgot Password?</a></span>
        </label>
        <button class="btn btn-primary btn-lg btn-block" type="submit">Login</button>
        <button class="btn btn-info btn-lg btn-block" type="reset">Signup</button>
    </div>
</form>

Error Image

错误图像

采纳答案by Abdulla Nilam

Error in the route you've defined. Its getand should change to post

您定义的路线有误。它get并且应该更改为post

change this

改变这个

Route::get('/login', ['as' => 'login', 'uses' => 'LoginController@getLogin']);

to this

对此

Route::post('/login', ['as' => 'login', 'uses' => 'LoginController@getLogin']);


action="{{ route('login') }}" # form Submit action

回答by zabbir hossain

Route::post('/', 'PostController@index');

Route::post('/posts/create', 'PostController@create');

Route::post('/posts', 'PostController@store');

Post or get..if u pass post value than use post.

Post 或 get..如果你传递 post 值而不是使用post

回答by danoj.I

your answer of the route should be

你对路线的回答应该是

Route::post('/login', ['as' => 'login', 'uses' => 'LoginController@getLogin']);

回答by devzakir

This problem appears only if you forget attached method on your form or error in Route method.

仅当您忘记表单上的附加方法或 Route 方法中的错误时才会出现此问题。

So be sure you added method POST/GET in your form. And don't forget make matching route.

因此,请确保您在表单中添加了 POST/GET 方法。并且不要忘记制作匹配路线。

<form method="POST">

If your form method is post. make post route like this.

如果您的表单方法是 post。像这样制作邮寄路线。

Route::post();

I hope you understand the method define. If you're facing problem, comment below.

我希望你理解方法定义。如果您遇到问题,请在下面发表评论。