解决 Entrust 的 Trait 冲突

WBOY
發布: 2016-06-20 12:37:02
原創
1175 人瀏覽過

因为有朋友在问我 [ here], 而我之前也正好遇到过,所以记录下。

当使用的多个 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 { restore as private restoreA; }    use 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;}
登入後複製

参考: Laravel 5.1.11 - Trait method can has not been applied, because there are collisions with other trait methods on App\User

來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!