Laravel5.1 ACL教程
按照上面的教程,使用者表使用預設的user表的話,一切都是很順利的,可是我最終的目的是背景admin表進行許可權管理,於是把教程裡的user都換成了admin,這時候出了問題,在blade中@can('edit-post')
和@can('delete-post')
全部沒通過
於是我換到了控制器
$admin = Auth::guard('admin')->user(); if($admin->can('edit-post')){ echo 1; }
通過,成功輸出1,yeah!
於是我就想到了會不會是模版中的can
是取的user表?於是我又把auth.php
中的預設guard
改成admin
'defaults' => [ 'guard' => 'admin', 'passwords' => 'users', ],
成功!yeah!
好了,問題來了,我不改auth.php
中guard
的預設值,怎樣可以在模版中讓can
判斷的是admin表的使用者?
回複內容:
Laravel5.1 ACL教程
按照上面的教程,使用者表使用預設的user表的話,一切都是很順利的,可是我最終的目的是背景admin表進行許可權管理,於是把教程裡的user都換成了admin,這時候出了問題,在blade中@can('edit-post')
和@can('delete-post')
全部沒通過
於是我換到了控制器
$admin = Auth::guard('admin')->user(); if($admin->can('edit-post')){ echo 1; }
通過,成功輸出1,yeah!
於是我就想到了會不會是模版中的can
是取的user表?於是我又把auth.php
中的預設guard
改成admin
'defaults' => [ 'guard' => 'admin', 'passwords' => 'users', ],
成功!yeah!
好了,問題來了,我不改auth.php
中guard
的預設值,怎樣可以在模版中讓can
判斷的是admin表的使用者?
看了下源碼,似乎並不支援!!!
/** * Compile the can statements into valid PHP. * * @param string $expression * @return string */ protected function compileCan($expression) { return ""; }
/** * Determine if the given ability should be granted for the current user. * * @param string $ability * @param array|mixed $arguments * @return bool */ public function check($ability, $arguments = []) { try { $result = $this->raw($ability, $arguments); } catch (AuthorizationException $e) { return false; } return (bool) $result; }
模板裡可以這樣寫,只不過很長,我把if裡邊的存到一個公用函數中了
@if(Gate::forUser(Auth::guard('admin')->user())->allows('$priv'))@endif