2016-09-22 96 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

謝謝喬,我非常感謝這 –