Laravel 5 MethodNotAllowedHttpException in RouteCollection.php line 201:

68,693

Solution 1

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.

Solution 2

You're using post method in the form but you're having get method in the routes.

So, Change the method to post in your routes

Note :

I recommend you to make use of the default form opening of Laravel like the below given which is always the best practise

{!! Form::open(array('url' => 'foo/bar')) !!}

{!! Form::close() !!}

Tips :

Read more on here and try to debug such things by comparing the methods and routes.

Form facade is not included in laravel 5 by default. You shall install it by

composer require "illuminate/html":"5.0.*"

and updating in the app.php.

I have written a blog which gives a breif about this installation.

Solution 3

In Routes web.php Your code is

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

which is wrong. Actually submitting data in POST method its array of data so you need to route through post instead of get. so correct code is

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

Follow this tutorial form Laracast it might helpful,
https://laracasts.com/series/laravel-from-scratch-2017/episodes/16

Solution 4

In routes.php, replace Route::get by Route::post.

Share:
68,693
deep singh
Author by

deep singh

Updated on May 22, 2020

Comments

  • deep singh
    deep singh almost 4 years

    I have a number of php files in my project:

    admin.blade.php: this files contains the admin form.

    When called it show the following error:

    MethodNotAllowedHttpException in RouteCollection.php line 201

    <h2>Please Log In To Manage</h2>
    <form id="form1" name="form1" method="post" action="<?=URL::to('/admin')?>">
       <input type="hidden" name="_token" value="{{ csrf_token() }}">
       User Name:<br />
       <input name="username" type="text" id="username" size="40" />
       <br /><br />
       Password:<br />
       <input name="password" type="password" id="password" size="40" />
       <br />
       <br />
       <br />
       <input type="submit" name="button" id="button" value="Log In" />
    </form>
    

    In route.php, this call is made:

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

    This is the function in student.php

    public function admin()
    {
        return View::make('student.admin');
        $validator = Validator::make($data = Input::all() , User::rules());
        if ($validator->fails())
        {
            return Redirect::back()->withErrors($validator)->withInput();
        }
        else
        {
            $check = 0;
            $check = DB::table('admin')->get();
            $username = Input::get('username');
            $password = Input::get('password');
            if (Auth::attempt(['username' => $username, 'password' => $password]))
            {
                return Redirect::intended('/');
            }
            return Redirect::back()->withInput()->withErrors('That username/password combo does not exist.');
        }
    }
    

    I don't know much about creating an admin area, I am just trying to create it.

  • Nehal Hasnayeen
    Nehal Hasnayeen almost 9 years
    Form facade is not included in laravel 5 default, you have to install laravelcollective package
  • Sulthan Allaudeen
    Sulthan Allaudeen almost 9 years
    @NehalHasnayeen Thanks for reminding i will update it in the answer :)
  • Sulthan Allaudeen
    Sulthan Allaudeen almost 9 years
    @deepsingh Are you getting return Input::all(); ?
  • deep singh
    deep singh almost 9 years
    MethodNotAllowedHttpException in RouteCollection.php line 201: by doing post to routes and by using form facde . it shows this again
  • Sulthan Allaudeen
    Sulthan Allaudeen almost 9 years
    Are you sure you are on the correct form and routes, Also try to change the method to get (Just a blind way)
  • deep singh
    deep singh almost 9 years
    @SulthanAllaudeen .. i didnt get you. .. mean how return Input::all();
  • Sulthan Allaudeen
    Sulthan Allaudeen almost 9 years
    @deepsingh I mean to say return Request::all();
  • deep singh
    deep singh almost 9 years
    thanks.. by using match.. error gone.. but it is returning to the admin login page after submitting the button rather than going to view page... what should i do of this
  • Nehal Hasnayeen
    Nehal Hasnayeen almost 9 years
    because you are returning the login view at 1st line in admin method, you should use 2 different methods or use condition to check the request type , & then process it
  • deep singh
    deep singh almost 9 years
    i mean it must go to home page... bt it is not going there
  • deep singh
    deep singh almost 9 years
    so what should i do of that line.. i am not getting.. should i delete that line or place it somewhere else.. or tell me the edited version of that plz.
  • deep singh
    deep singh almost 9 years
    Class 'App\Http\Controllers\User' not found.. what of this @Nehal hasnayeen
  • Nehal Hasnayeen
    Nehal Hasnayeen almost 9 years
    You have to import user class ,where is your user class,i assume it is in app folder,if it is then import it like these : use App\User;
  • deep singh
    deep singh almost 9 years
    BadMethodCallException in Builder.php line 1994: it shows this now
  • Nehal Hasnayeen
    Nehal Hasnayeen almost 9 years
    give full error message, it must be showing which method
  • deep singh
    deep singh almost 9 years
    BadMethodCallException in Builder.php line 1994: Call to undefined method Illuminate\Database\Query\Builder::rules().. it says this
  • Nehal Hasnayeen
    Nehal Hasnayeen almost 9 years
    problem is in this line $validator = Validator::make($data = Input::all(), User::rules()); there is no rules method,rather you should give an array of rules for validation
  • deep singh
    deep singh almost 9 years
    omg. lot of errors . it shows this this now. ErrorException in Factory.php line 91: Argument 2 passed to Illuminate\Validation\Factory::make() must be of the type array, none given, called in C:\xampp\htdocs\again\vendor\laravel\framework\src\Illuminat‌​e\Support\Facades\Fa‌​cade.php on line 210 and defined
  • Nehal Hasnayeen
    Nehal Hasnayeen almost 9 years
    i told you to give an array of rules for validation , read carefully previous comments
  • deep singh
    deep singh almost 9 years
    ErrorException in Factory.php line 91: Argument 2 passed to Illuminate\Validation\Factory::make() must be of the type array, none given, called in C:\xampp\htdocs\again\vendor\laravel\framework\src\Illuminat‌​e\Support\Facades\Fa‌​cade.php on line 210 and defined.. it shows this error now.. can you plz help me in this @SulthanAllaudeen
  • Sulthan Allaudeen
    Sulthan Allaudeen almost 9 years
    @deepsingh You still have the issue ?
  • deep singh
    deep singh almost 9 years
    no thanks.. i have solved this .. bt i have other issues plz go on this link.. and read the comnts and then make me reply brother stackoverflow.com/questions/31233896/…
  • AMIC MING
    AMIC MING about 6 years
    Thanks for the answer. Nice Solutions