解決 Entrust 的 Trait 沖突

分類:IT技術 時間:2017-02-15

當使用的多個 trait 中包含了相同的方法名,將會發生沖突,沖突錯誤信息如下:

FatalErrorException in User.php line 43:
Trait method xxxxxx has not been applied, because there are collisions with other trait methods on App\Http\models\User

和 SoftDeletes 的 restore 沖突

由於 EntrustUserTrait 和 SoftDeletes 兩個 trait 都包含 restore 方法,所以當我們對用戶 Model 使用軟刪除的時候同時集成 Entrust 的時候就會導致沖突。

解決方法就是引用兩個 trait 時為 restore 方法設置別名,然後重寫一個 restore 方法,分別調用兩個restore 方法。代碼如下:

class User extends Model implements Authenticatableinterface
{
    use Authenticatable;
    use EntrustUserTrait { EntrustUserTrait::restore as private restoreA; }
    use SoftDeletes { SoftDeletes::restore as private restoreB; }

    /**
     * 解決 EntrustUserTrait 和 SoftDeletes 沖突
     */
    public function restore()
    {
        $this->restoreA();
        $this->restoreB();
    }
}

和 Authorizable 的 can 沖突

解決辦法是將 EntrustUserTrait 的 can 方法改一個別名,然後使用 Authorizable 中的 can,代碼如下

use Authenticatable, CanResetPassword, PresentableTrait, Authorizable, EntrustUserTrait {
    EntrustUserTrait::can as may;
    Authorizable::can insteadof EntrustUserTrait;
}

 


Tags: function because private methods public

文章來源:


ads
ads

相關文章
ads

相關文章

ad