2016-06-14 13:48
浏览 138

Laravel - 如何使用Laravel在另一个控制器中提供的AuthController注册新用户

So I am building a checkout functionality. If the user is a guest he could use his credentials to make a new registration with the order he makes. I have used the auth scaffold Laravel provides with the command php artisan make:auth. I have an OrdersController and the AuthController that comes with the framework. How can I register a new user within my OrdersController? Here is my code: checkout.blade.php:

<li class="col-md-12 col-sm-12">

    <input id="create-act" type="checkbox" class="input-chkbox" name="register">
    <label for="create-act">Create user account?</label>
<li class="col-md-12 col-sm-12 create-account">

    <label for="password">Password<em>*</em></label>
    <input required type="text" class="input-text" id="password"
<li class="col-md-12 col-sm-12 create-account">
    <label for="password_confirmation">Confirm Password<em>*</em></label>
    <input required type="text" class="input-text"

OrdersController inside addOrder method:

if(isset($_POST['register'])) {
 $this->validate($request, [
        'password' => 'required|password',
        'confirm_password' => 'required|'

I want to register the new user inside this if. How can I do that?

Edit: Here is my protected function create inside my AuthController.

protected function create(array $data)
    $confirmationCode = str_random(30);

    $user = User::create([
        'name' => $data['name'],
        'email' => $data['email'],
        'password' => bcrypt($data['password']),
        'confirmation_code' => $confirmationCode

    $user->roles()->attach(Role::where('name', 'Customer')->first());
    $cart = new Cart();
    $cart->user_id = $user->id;
    if(Session::has('cartItems')) {
        foreach (Session::get('cartItems') as $sessionCartItem) {
            $cartItem = new CartItem();
            $cartItem->product_id = $sessionCartItem['product']->id;
            $cartItem->cart_id = $cart->id;
            $cartItem->size = $sessionCartItem['size'];
            $cartItem->quantity = $sessionCartItem['quantity'];


    $email = $user['email'];
    $name = $user['name'];

    Mail::send('auth.emails.user-confirmation', ['name' => $name, 'confirmation_code' => $user['confirmation_code']], function ($message) use ($email, $name) {
        $message->from('mymail@gmail.com', 'Name Family')->to($email, $name)->subject('Confirm email');

    return $user;
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • doulvyi2172
    doulvyi2172 2016-06-16 12:44

    I didn't want my OrdersController to extend my AuthController, was to lazy to extract the authentication logic into a library and just redirecting if the register checkbox was checked didn't work. I just decided to duplicate part of my registration logic inside my OrdersController. It is not good practice for sure, but it is a workaround until I extract the login into a library. Here is the code:

    if (isset($_POST['register'])) {
        $this->validate($request, [
           'password' => 'required|min:6|confirmed',
           'password_confirmation' => 'required'
        $user = User::create([
            'name' => $request['name'],
            'email' => $request['email'],
            'password' => bcrypt($request['password']),
            'phone_number' => $request['phone']
    点赞 评论
  • duanmao2774
    duanmao2774 2016-06-14 13:55

    The AuthController does a simple User::create($data); after the validation is done. Optionally you could do Auth::guard()->login($user); which will automatically login the newly created user.

    点赞 评论
  • drdawt9210
    drdawt9210 2016-06-14 14:01

    Normally, you can easily create user with the request:

    $user = User::create($request->all());

    Make sur that the $request variable matches the columns of users table.

    EDIT: To call a function from another controller:

    class OrdersController extends AuthController
    点赞 评论
  • douti0467
    douti0467 2016-06-14 14:07

    After you validation succeeds Fetch out all the data from $request that you need for you user table, use the User model to insert a column,

    you can use


    or you can use


    Than you can use the Auth Guard method to login the user


    Edit : Create a library function and use it where ever you want

    namespace App\Lib\User;
    class Creator
        public function create(Request $request)
           // do what you want
          // return what you want

    Than use this function in order controller or auth controller

    use App\Lib\User\Creator;
     public function __construct(Creator $creator) {
     public function register(){
    点赞 评论