Laravel 5 MethodNotAllowedHttpException in RouteCollection.php line 201:

This is how I do it.

Routes.php

Route::get('/admin', 'UsersController@getAdminLogin');
Route::get('/admin/dashboard', 'UsersController@dashboard');
Route::post('/admin', 'UsersController@postAdminLogin');

admin_login.blade.php

{!! Form::open(['url' => '/admin']) !!}
    <div class="form-group">
        {!! Form::label('email', 'Email Id:') !!}
        {!! Form::text('email', null, ['class' => 'form-control input-sm']) !!}
    </div>
    <div class="form-group">
        {!! Form::label('password', 'Password') !!}
        {!! Form::password('password', ['class' => 'form-control input-sm']) !!}
    </div>
    <div class="form-group">
        {!! Form::submit('Login', ['class' => 'btn btn-primary btn-block']) !!}
    </div>
{!! Form::close() !!}

dashboard.blade.php

<h4 class="text-center">
    Welcome {{ Auth::user()->full_name }}
</h4>

UsersController.php

/**
 * Display the admin login form if not logged in,
 * else redirect him/her to the admin dashboard.
 *
 */
public function getAdminLogin()
{
    if(Auth::check() && Auth::user()->role === 'admin')
    {
        return redirect('/admin/dashboard');
    }
    return view('admin_login');
}

/**
 * Process the login form submitted, check for the
 * admin credentials in the users table. If match found,
 * redirect him/her to the admin dashboard, else, display
 * the error message.
 *
 */
public function postAdminLogin(Request $request)
{
    $this->validate($request, [
        'email'    => 'required|email|exists:users,email,role,admin',
        'password' => 'required'
    ]);

    $credentials = $request->only( 'email', 'password' );

    if(Auth::attempt($credentials))
    {
        return redirect('/admin/dashboard');
    }
    else
    {
        // Your logic of invalid credentials.
        return 'Invalid Credentials';
    }
}

/**
 * Display the dashboard to the admin if logged in, else,
 * redirect him/her to the admin login form.
 *
 */
public function dashboard()
{
    if(Auth::check() && Auth::user()->role === 'admin')
    {
        return view('admin.dashboard');
    }
    return redirect('/admin');
}

Your Code:

In routes.php, you have only 1 route, i.e.,

Route::get('/admin',array('uses'=>'student@admin'));

And there is no declaration of post method, hence, the MethodNotAllowedHttpException

Also, in your controller, you are returning the view first and then processing the form which is not going to work at all. You first need to process the form and then return the view.

public function admin(){
    // Won't work as you are already returning the view
    // before processing the admin form.
    return \View::make(students.admin);
    // ...
}

As @Sulthan has suggested, you should use Form Facade. You can check out this video on Laracasts about what Form Facade is and how you should use it.

Leave a Comment