2016-09-22 97 views
2

我正在与Laravel 5.3合作,我试图在某人注册时设置角色,我使用了Zizaco Entrust库。Laravel 5.3 - 在注册时实现委托作用的最佳方式?

我不确定要达到这样的效果的最佳方法。

我试图做类似下面这里面RegisterControllercreate方法:

protected function create(array $data) 
{ 
    return User::create([ 
     'name' => $data['name'], 
     'email' => $data['email'], 
     'password' => bcrypt($data['password']), 
    ]); 

    $user = User::where('email', '=', $data['email'])->first(); 

    // role attach alias 
    $user->attachRole($employee); 
} 

但很明显,这是不正确的。所以我有点不确定这种事情的最佳做法。

+0

你就不能在此设置了'__construct'的'Model'的? –

+0

我不确定它没有被添加到用户表中,它使用用户的ID来创建外键 –

+0

您是否想根据他们选择的内容来分配不同的角色?总是被赋予一定的作用? – Joe

回答

2

如果您对OP的建议建议您始终希望将相同的角色分配给注册用户,那么可以使用模型观察器来实现此目的 - 这非常简单。

// app/Observers/UserObserver.php 

<?php namespace App\Observers; 

use App\Models\User; 
use App\Models\Role; // or the namespace to the Zizaco Role class 

class UserObserver { 

    public function created(User $user) { 
     $role = Role::find(1); // or any other way of getting a role 
     $user->attachRole($role); 
} 

然后你只需注册观察者在AppServiceProvider:

// app/Providers/AppServiceProvider.php 

use App\Models\User; 
use App\Observers\UserObserver; 

class AppServiceProvider extends Provider { 

    public function boot() { 
     User::observe(new UserObserver); 
     // ... 
    } 

    // ... 

} 
+0

这工作完美,谢谢! –

0

我最终做了什么,因为我确实需要做的不仅仅是对用户创建的一个操作,而是为用户创建创建了另一个功能。

用户模型

/** 
* Create a new user instance after a valid registration. 
* 
* @param array $attributes 
* @param null $role 
* @param bool $send_activation_email 
* 
* @return User $user 
* 
* @internal param array $args 
*/ 
public function createNew(array $attributes, $role = null, $send_activation_email = true) 
{ 
    $this->name = $attributes['name']; 
    $this->company_id = $attributes['company_id']; 
    $this->email = $attributes['email']; 
    $this->password = bcrypt($attributes['password']); 
    $this->save(); 

    if (isset($role)) { 
     // Assigning the role to the new user 
     $this->attachRole($role); 
    } 

    //If the activation email flag is ok, we send the email 
    if ($send_activation_email) { 
     $this->sendAccountActivationEmail(); 
    } 

    return $this; 
} 

,把它想:

用户控制器

$user = new User(); 
$user->createNew($request->all(), $request->role); 

它可能不是最好的解决办法,但它确实工作,并且它是未来的教授,所以如果创建用户的逻辑也可以实现。

+0

这是修改'RegisterController.php'吗? –

+0

第一位在用户模型中。创建它的调用在Controller中。希望它是有道理的 –

+0

Gotcha,所以你正在采取创建方法,并在模型中修改它,我明白 –

1

这个答案主要是基于你目前的解决方案,用一些原始问题。

与使用createNew等方法填充模型不同,如果创建专门用于与模型进行交互的类的类型,则可能会发现管理更容易。您可以将此称为存储库或服务或任何您喜欢的东西,但我们将使用服务运行。

// app/Services/UserService.php 

<?php namespace App\Services; 

use App\Models\User; // or wherever your User model is 

class UserService { 

    public function __construct(User $user) { 
     $this->user = $user; 
    } 

    public function create(array $attributes, $role = null) { 
     $user = $this->user->create($attributes); 

     if ($role) { 
      $user->attachRole($role); 
     } 

     return $user; 
    } 

} 

现在,我们需要处理的事实,我们已经失去了密码的哈希:

// app/Models/User.php 
class User ... { 

    public function setPasswordAttribute($password) { 
     $this->attributes[ 'password' ] = bcrypt($password); 
    } 

} 

现在我们必须发出一封激活邮件的问题 - 可以清晰地得到解决与事件。在终端运行以下命令:

php artisan make:event UserHasRegistered 

,它应该是这个样子:

// app/Events/UserHasRegistered.php 

<?php namespace App\Events; 

use App\Models\User; 
use Illuminate\Queue\SerializesModels; 

class UserHasRegistered extends Event { 

    use SerializesModels; 

    public $user; 

    public function __construct(User $user) { 
     $this->user = $user; 
    } 

} 

现在我们需要为事件侦听器:

php artisan make:listener SendUserWelcomeEmail 

,这可以是复杂如你所愿,下面是我刚刚从一个项目中复制/粘贴:

// app/Listeners/SendUserWelcomeEmail.php 

<?php namespace App\Listeners; 

use App\Events\UserHasRegistered; 
use App\Services\NotificationService; 

class SendUserWelcomeEmail { 

    protected $notificationService; 

    public function __construct(NotificationService $notificationService) { 
     $this->notify = $notificationService; 
    } 

    public function handle(UserHasRegistered $event) { 
     $this->notify 
      ->byEmail($event->user->email, 'Welcome to the site', 'welcome-user') 
      ->send(); 
    } 

} 

剩下的就是告诉Laravel我们刚刚创建的Event和Listener是相关的,然后发起事件。

// app/Providers/EventServiceProvider.php 

use App\Events\UserHasRegistered; 
use App\Listeners\SendUserWelcomeEmail; 

class EventServiceProvider extends ServiceProvider { 

    // find this array near the top, and add this in 
    protected $listen = [ 
     UserHasRegistered::class => [ 
      SendUserWelcomeEmail::class, 
     ], 
    ]; 

    // ... 

} 

现在我们只需要提出事件 - 请参阅我的另一篇关于Model Observers的文章。首先,您需要导入EventApp\Events\UserHasRegistered,然后在您的created方法中,请拨打Event::fire(new UserHasRegistered($user))

+0

什么是一个很好的答案。当我下班回家时,我会放弃这一点。看起来非常好 –

+1

@AndyHolmes我刚刚为你写了另一篇 - Laravel有很多做事的方式。如果这个角色是已知的,那么另一种方法对于那个特定的东西更好,尽管我建议使用mutator作为你的密码属性以及电子邮件的事件/侦听器设置。如果你发现自己需要在模型上执行更复杂的动作,服务也是一个很好的方法, – Joe

+0

谢谢乔,我非常感谢这 –