如何在 Laravel 5.2 中自定义注册
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36522534/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to custom registration in Laravel 5.2
提问by Boris Dehoumon
I want to custom register function in Laravel 5.2, before it was in the postRegister()
function in Laravel 5.2 but there is more that function.
I wanted to know how?
Thank you
我想在 Laravel 5.2 中自定义注册函数,之前它是postRegister()
在 Laravel 5.2中的函数中,但还有更多该函数。我想知道怎么做?谢谢
回答by trinvh
If you want add custom attribute when user register, see first answer via @Achraf.
如果您想在用户注册时添加自定义属性,请通过@Achraf 查看第一个答案。
If you want send confirmation email, this should works:
如果您想发送确认电子邮件,这应该有效:
class AuthController extends Controller {
// override default register method
public function register(Request $request) {
$validator = $this->validator($request->all());
if ($validator->fails()) {
$this->throwValidationException(
$request, $validator
);
}
$user = $this->create($request->all());
// Sending email, sms or doing anything you want
$this->activationService->sendActivationMail($user);
return redirect('/login')->with('message', 'We sent a comfirmation email to your email, please click on link inside before login');
}
}
回答by Achraf Khouadja
'but there is more that function?' i dont realy understand what u mean
“但还有更多功能吗?” 我不太明白你的意思
anyway check this LARAVEL API
无论如何检查这个 LARAVEL API
and here is a little exemple i hope it helps :
这是一个小例子,我希望它有帮助:
namespace App\Http\Controllers\Auth;
use App\User;
use App\Userinfo;
use Validator;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
class AuthController extends Controller
{
use AuthenticatesAndRegistersUsers, ThrottlesLogins;
protected $redirectTo = '/';
public function __construct()
{
$this->middleware('guest', ['except' => 'logout']);
}
protected function validator(array $data)
{
return Validator::make($data, [
'firstname' => 'required|max:255',
'lastname' => 'required|max:255',
'email' => 'required|email|max:255|unique:users',
'address' => 'required|max:255',
'city' => 'required|max:255',
'country' => 'required|max:255',
'zip' => 'required|max:255',
]);
}
protected function create(array $data)
{
$user = User::create([
'firstname' => $data['firstname'],
'lastname' => $data['lastname'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
$userinfo = Userinfo::create([
'user_id' => $user->id,
'address' => $data['address'],
'address2' => $data['address2'],
'city' => $data['city'],
'zip' => $data['zip'],
'country' => $data['country'],
'phone' => $data['phone'],
]);
return ($user);
}
}
as ou can see i have 2 tables, user and userinfos
正如你所看到的,我有 2 个表,user 和 userinfos
Check this exemple , it should help you ->TAKE ME TO REDEMPTION
检查这个例子,它应该可以帮助你 ->TAKE ME TO REDEMPTION
This tutorial will help too ->potatos
本教程也会有所帮助 ->potatos