Yii 2将所有请求路由到无效请求页面期望某些操作

I have 2 controllers with some actions, I want to all requests expect those actions in the 2 controllers to be routed to invalid request page, how can I do that?

suppose:

controller1 => action1, action2, action3 allowed

controller2 => action4, action5, action6 allowed

all other requests should be go to bad request page.

Thank you.

As gmc specified you should use Access Control , however you might want to specify the "bad request" page.

public function behaviors()
{
    return [
        'access' => [
            'class' => AccessControl::className(),
            'only' => ['action1', 'action2', 'action3'],
            'rules' => [
                [
                    'allow' => true,
                ],
            ],
            'denyCallback' => function($rule, $action) {
                 return $this->redirect(['controller/action']);
            }
        ],
    ];
}

Use Access Control, e.g. for controller 1:

public function behaviors()
{
    return [
        'access' => [
            'class' => \yii\filters\AccessControl::className(),
            'only' => ['action1', 'action2', 'action3'],
            'rules' => [
                [
                    'allow' => true,
                ],
                // everything else is denied
            ],
        ],
    ];
}