9

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

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

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

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

Middleware/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

4 Answers 4

8

Error in the route you've defined. Its get and should change to 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
Sign up to request clarification or add additional context in comments.

4 Comments

Oh - i do not see it, : )
When your form requesting post and route define as get, it will throw this http error
When I login It Should go to dashboard.
define redirect on success
3
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.

Comments

2

your answer of the route should be

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

Comments

1

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

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

<form method="POST">

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

Route::post();

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.