0

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!

4

2 回答 2

3

似乎您只是在路由该GET方法。您还应该路由该POST方法以使您的控制器工作:

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

通常,最好必须在控制器中分离方法来处理不同的逻辑。如下:

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

这样,您将拥有更具体的控制器方法,一个仅显示登录页面,另一个实际执行登录过程。


更新

如果你想在 Laravel 中检索输入值,你应该使用这个Input类,所以你将替换你当前的代码:

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

如下:

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

快乐编码!

于 2013-09-05T14:37:42.397 回答
2

您需要将表单方法指定为 POST 以便读取

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

在您的路线文件中添加以下内容

Route::post('/login', array('uses' => 'LoginController@login')
于 2013-09-05T14:41:59.223 回答