在控制器,你可以使用訪問控制過濾器(ACF)
假設你在你的網站控制器希望只允許角色admin訪問actionViewforadmin
use yii\web\Controller;
use yii\filters\AccessControl;
class SiteController extends Controller
{
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::className(),
'only' => ['login', 'logout', 'signup', 'viewforadmin'],
'rules' => [
[
'allow' => true,
'actions' => ['login', 'signup'],
'roles' => ['?'],
],
[
'allow' => true,
'actions' => ['logout'],
'roles' => ['@'],
],
[
'allow' => true,
'actions' => ['viewforadmin'],
'roles' => ['admin'],
],
],
],
];
}
// ...
}
你可以看看這個簡短引導
http://www.yiiframework.com/doc-2.0/guide-security-authorization.html或該參考http://www.yiiframework.com/doc-2.0/yii-base-actionfilter.html
你有沒有使用[訪問控制過濾器(HTTP認爲:// WWW。 yiiframework.com/doc-2.0/guide-security-authorization.html#access-control-filter)? – Bizley
是的,我做了@Bizley ...但我想要返回某個行爲的結果,不管是哪個行爲被調用,不要禁止其他行爲。我還沒有找到一種方法來做到這一點與訪問控制... – thomas