我有登录和注册表格,两者都有效。它们在同一页上,
登录表单使用:
{{ Form::open(array('url' => 'login')) }}
并注册用途:
{{ Form::open(array('url' => 'register')) }}
然后将它们定向到HomeController中的不同函数,成功后它们返回成员视图。
我的问题是,我没有将用户定向到url.com/member,而是将url.com/login用于登录,并将url.com/register注册为会员视图的网址。
有没有正确的方法呢?我一直在浏览文档,我遇到过
Redirect::url('member')
作为一个选项,但是如果我同时执行注册和登录功能,我会得到一个http路由错误异常,这不允许我这样做。
如果有人能帮助我,那就太好了。
此外,这是我的注册和登录功能:
登录:
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');
return View::make('member');
} else {
// validation not successful, send back to form
return Redirect::to('/');
}
}
}
寄存器:
HomeController功能:
public function doRegister()
{
$name = Input::get('name');
$email = Input::get('email');
$password = Input::get('password');
$userData = array(
array('name' => $name),
array('email' => $email),
array('password' => $password),
);
User::addNewUser($name, $email, $password);
return View::make('member');
}
用户模型:
public static function addNewUser($name, $email, $password)
{
DB::table('users')->insert(array('name' => $name, 'email' => $email, 'password' => Hash::make($password)));
}
路线:
// route to show the login form
Route::get('/', array('uses' => 'HomeController@showHome'));
//Route::get('/register', array('uses' => 'HomeController@showRegister'));
// route to process the form
Route::post('login', array('uses' => 'HomeController@doLogin'));
Route::post('register', array('uses' => 'HomeController@doRegister'));
答案 0 :(得分:1)
为了做到这一点:
return Redirect::to('member')
您需要在routes.php
中定义该路线,如下所示:
// routes.php
...
Route::get('member', function()
{
return View::make('member');
});
...
现在您已经定义了它,您可以在doLogin
和doRegister
操作中使用它:
// HomeController.php
...
if (Auth::attempt($userdata)) {
return Redirect::to('member');
}
...
另请注意,在验证失败时,不是将重定向硬编码到'/'
,而是返回Redirect::back()
。