在 Yii 框架中的应用程序中声明过滤器

Declaring filter in application in Yii framework

在Yii中doc,据说

Besides controllers, you can also declare filters in a module or application.

但是,如何在应用程序中以编程方式实现它?

您可以在其配置中附加应用程序的行为。 例如,在 frontend\config\main.php:

中附加 ContentNegotiator
return [
    'as contentNegotiator' => [
        'class' => \yii\filters\ContentNegotiator::class,
        'formats' => [
            'application/json' => \yii\web\Response::FORMAT_JSON,
            'application/xml' => \yii\web\Response::FORMAT_XML,
        ],
        'languages' => [
            'en-US',
            'de',
        ],
    ],
    // ... other configuration ...
];