我有一个登录页面并想要在同一页面上设置表单。
登录表单工作得很好,已经将伪数据输入到我正在使用的数据库中。
我遇到的问题是,我收到了一个方法调用错误(可能是因为我对两个不同的函数进行了相同的post函数调用。
目前在我的routes.php文件中
// route to process the form
Route::post('/', array('uses' => 'HomeController@doLogin'));
Route::post('/', array('uses' => 'HomeController@doRegister'));
我的控制器文件看起来像这样(很抱歉,它有点长,我认为最好提供所有内容,而不是假设某人仅凭我的解释就能理解我的问题)
public function doRegister() {
$v = User::validate(Input::all());
if ( $v->passes() ) {
User::create(array(
'name'=> Input::get('name'),
'email'=> Input::get('email'),
'password'=> Hash::make(Input::get('password')),
));
return 'Thanks for registering!';
} else {
return Redirect::to('/')->withErrors($v->getMessages());
}
}
public function doLogin()
{
// validate the info, create rules for the inputs
$rules = array(
'email' => 'required|email', // make sure the email is an actual email
'password' => 'required|alphaNum|min:3' // password can only be alphanumeric and has to be greater than 3 characters
);
// run the validation rules on the inputs from the form
$validator = Validator::make(Input::all(), $rules);
// if the validator fails, redirect back to the form
if ($validator->fails()) {
return Redirect::to('/')
->withErrors($validator) // send back all errors to the login form
->withInput(Input::except('password')); // send back the input (not the password) so that we can repopulate the form
} else {
// create our user data for the authentication
$userdata = array(
'email' => Input::get('email'),
'password' => Input::get('password')
);
// attempt to do the login
if (Auth::attempt($userdata)) {
// validation successful!
// redirect them to the secure section or whatever
// return Redirect::to('secure');
// for now we'll just echo success (even though echoing in a controller is bad)
echo 'SUCCESS!';
} else {
// validation not successful, send back to form
return Redirect::to('/');
}
}
}
据我所知,这是因为我没有为注册表格设置要正确使用的功能。
希望我在解释我的问题上做得很好,请问有什么解决方案吗?(对laravel来说还不是很新)
一种形式将张贴登录,另一种形式进行注册
Route::post('login', array('uses' => 'HomeController@doLogin'));
Route::post('register', array('uses' => 'HomeController@doRegister'));
然后,您将打开以下表单:
{{ Form::open(array('url' => 'login')) }}
和
{{ Form::open(array('url' => 'register')) }}
编辑:例如,表单仅放置在您的主视图中,然后您只需从登录和注册方法重定向,而不显示视图。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句