web-dev-qa-db-ja.com

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

これは私の頭にあります。フォームからログインしようとすると、このエラーが表示されます。

Symfony \コンポーネント\ HttpKernel \例外\

MethodNotAllowedHttpException

メッセージなし

LoginController.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('/');
    }
}

フォームを送信するときの次のルートは正常に機能しています。

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

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

ミドルウェア/Roles.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

<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

6
user9085794

定義したルートのエラー。そのgetpostに変更する必要があります

これを変える

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

これに

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

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

ルートの答えは

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