所以我想了解Laravel 5.1中的用戶授權。Laravel 5.1調用未定義的方法Illuminate Database Query Builder :: isSuperAdmin()
基礎上docs我已經設置了AuthServiceProvider引導方法如下:
public function boot(GateContract $gate)
{
parent::registerPolicies($gate);
$gate->define('view-dashboard', function ($user, $post) {
return $user->id === $post->user_id;
});
$gate->before(function ($user, $ability) {
if($user->isSuperAdmin()) {
return true;
}
});
}
在我的控制,我有:
if (Gate::denies('view-dashboard')) {
return view('auth.login');
}
return view('admin.home');
當我沒有登錄我得到的auth.login視圖。但是,一旦我登錄,我得到了以下錯誤:
BadMethodCallException in Builder.php line 2025: Call to undefined method Illuminate\Database\Query\Builder::isSuperAdmin()
首先,因爲我把這些線直出的文檔的,我不知道我爲什麼會得到這個錯誤。有任何想法嗎?其次,文檔似乎沒有解釋如何去指定給定用戶作爲超級管理員,或者如何給予用戶特定的能力(例如我的示例中的「視圖 - 儀表板」能力)。我該怎麼做呢?
UPDATE:這是我的用戶模型:
namespace App;
use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Foundation\Auth\Access\Authorizable;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
class User extends Model implements AuthenticatableContract,
AuthorizableContract,
CanResetPasswordContract
{
use Authenticatable, Authorizable, CanResetPassword;
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'users';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = ['name', 'email', 'password'];
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = ['password', 'remember_token'];
}
嗯,我的config/app.php中沒有routeMiddleware。我確實在app \ Http \ Kernel.php中找到了$ routeMiddleware。 爲了增加'superadmin'=> \ App \ Http \ Middleware \ SuperAdminMiddleware :: class 但它仍然拋出相同的錯誤 –
更新了我的答案。讓我知道 – verheesj
是的,我的用戶模型實現了所有這些(我已經更新了我的問題以包含用戶模型) –