dsgwii4867 2016-06-14 13:48
浏览 160
已采纳

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">
    <h4>Register</h4>

    <input id="create-act" type="checkbox" class="input-chkbox" name="register">
    <label for="create-act">Create user account?</label>
</li>
<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"
           name="password">
</li>
<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"
           id="password_confirmation"
           name="password_confirmation">
</li>

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;
    $cart->save();
    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'];
            $cartItem->save();
        }
    }

    Session::flush();
    $cart->save();

    $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 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']
        ]); 
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 求TYPCE母转母转接头24PIN线路板图
  • ¥100 国外网络搭建,有偿交流
  • ¥15 高价求中通快递查询接口
  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型