[ "class" => AccessControl::className(), " />
牛骨文教育服务平台(让学习变的简单)

Action Filter

Filter

请参考 http://www.yiiframework.com/doc-2.0/guide-structure-filters.html

Filter是一种特殊的Behavior。

AccessControl

public function behaviors()
{
    return [
        "access" => [
            "class" => AccessControl::className(),
            "only" => ["create", "update"],
            "rules" => [
                // allow authenticated users
                [
                    "allow" => true,
                    "roles" => ["@"],
                ],
                // everything else is denied by default
            ],
        ],
    ];
}

ContentNegotiator

public function behaviors()
{
    return [
        [
            "class" => ContentNegotiator::className(),
            "formats" => [
                "application/json" => Response::FORMAT_JSON,
                "application/xml" => Response::FORMAT_XML,
            ],
            "languages" => [
                "en-US",
                "de",
            ],
        ],
    ];
}

HttpCache

public function behaviors()
{
    return [
        [
            "class" => HttpCache::className(),
            "only" => ["index"],
            "lastModified" => function ($action, $params) {
                $q = new yiidbQuery();
                return $q->from("user")->max("updated_at");
            },
        ],
    ];
}

PageCache

public function behaviors()
{
    return [
        "pageCache" => [
            "class" => PageCache::className(),
            "only" => ["index"],
            "duration" => 60,
            "dependency" => [
                "class" => DbDependency::className(),
                "sql" => "SELECT COUNT(*) FROM post",
            ],
            "variations" => [
                Yii::$app->language,
            ]
        ],
    ];
}

VerbFilter

public function behaviors()
{
    return [
        "verbs" => [
            "class" => VerbFilter::className(),
            "actions" => [
                "index"  => ["get"],
                "view"   => ["get"],
                "create" => ["get", "post"],
                "update" => ["get", "put", "post"],
                "delete" => ["post", "delete"],
            ],
        ],
    ];
}

Cors

public function behaviors()
{
    return ArrayHelper::merge([
        [
            "class" => Cors::className(),
            "cors" => [
                "Origin" => ["http://www.myserver.net"],
                "Access-Control-Request-Method" => ["GET", "HEAD", "OPTIONS"],
            ],
            "actions" => [
                "login" => [
                    "Access-Control-Allow-Credentials" => true,
                ]
            ]
        ],
    ], parent::behaviors());
}