drmlxgmqn18198265 2015-07-05 09:05
浏览 68
已采纳

RouteCollection.php第201行中的Laravel 5 MethodNotAllowedHttpException:

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.

  • 写回答

5条回答 默认 最新

  • duancu4731 2015-07-05 10:32
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 软件供应链安全是跟可靠性有关还是跟安全性有关?
  • ¥15 电脑蓝屏logfilessrtsrttrail问题
  • ¥20 关于wordpress建站遇到的问题!(语言-php)(相关搜索:云服务器)
  • ¥15 【求职】怎么找到一个周围人素质都很高不会欺负他人,并且未来月薪能够达到一万以上(技术岗)的工作?希望可以收到写有具体,可靠,已经实践过了的路径的回答?
  • ¥15 Java+vue部署版本反编译
  • ¥100 对反编译和ai熟悉的开发者。
  • ¥15 带序列特征的多输出预测模型
  • ¥15 Python 如何安装 distutils模块
  • ¥15 关于#网络#的问题:网络是从楼上引一根网线下来,接了2台傻瓜交换机,也更换了ip还是不行
  • ¥15 资源泄露软件闪退怎么解决?