drau89457 2013-09-05 14:24
浏览 153
已采纳

无法使用简单的Laravel Login控制器设置

I am just starting with Laravel after finally deciding to move from CodeIgniter, however I cannot get a simple login form to work. I keep getting that "MethodNotAllowedHttpException" error.

Here is how my Controller looks

class LoginController extends BaseController {

    public function login()
    {
        $username = $_POST['username'];
        $password = $_POST['password'];
        Login::login($username, $password); 
        // ^ Call to Login model to check the user's credentials - everything fine there
    }

}

Here is my view

{{ Form::open(array('url' => 'LoginController/login')) }}
        {{ Form::text('username') }}
        {{ Form::password('password') }}
        {{ Form::submit('Submit') }}
{{ Form::close }}

And my route for this is like

Route::get('LoginController/login', 'LoginController@login');

I'm doing something horribly wrong somewhere, can you guys please point it out?

Thanks!

  • 写回答

2条回答 默认 最新

  • doucheng5209 2013-09-05 14:37
    关注

    Seems like you are just routing the GET method. You should also route the POST method to make your controller work:

    Route::get('LoginController/login', 'LoginController@login');
    Route::post('LoginController/login', 'LoginController@login');
    

    Normally, It is better to have to separate methods in your controller to handle different logic. Something as follow:

    Route::get('login', 'LoginController@showLogin');
    Route::post('login', 'LoginController@processLogin');
    

    That way you will have your controller's methods more specific, one that just show the login page, and the other that actually do the login process.


    Update

    If you want to retrieve input values within Laravel, you should use the Input class, so you will replace your current code:

    $username = $_POST['username'];
    $password = $_POST['password'];
    

    to be as follow:

    $username = Input::get('username');
    $password = Input::get('password');
    

    Happy coding!

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

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大