Yii1 - 在之前的操作中清理 GET 参数

Yii1 - sanitize GET parameter in the before action

我正在尝试寻找是否可以在控制器中使用 beforeAction 来访问注入的参数。

例如,控制器中的每个动作都接受 type 参数,我需要对其进行清理:

public function actionGetCustomPaymentsChunk($type) {

        $type = TextUtil::sanitizeString($type);

        // Get filter data
        $filter = FilterHelper::getFilterData();
        // Initialize components
        $totalCostPayableComponent = new TotalCostPayableComponent($filter);
        // Get chunk data
        $data = $totalCostPayableComponent->getCustomPaymentChunk($type);

        // Return content to client side
        $this->renderPartial('/partials/_payable_cost_chunk', array(
            'data'        => $data,
            'route'       => 'totalCostPayable/getCustomPaymentsGrid',
            'type'        => $type,
            'paymentType' => 'Custom',
        ));
    }
}

这是否可行(我正在努力避免重复)?

你应该可以,你试过什么?

假设 $type 是通过 GET 传递的,您可以在 beforeAction 中修改它,修改后的值将通过

这样的请求应用于目标操作

http://myhost.com/route/test?type=something

在此控制器中的任何操作中使用以下内容,$type = "foo"

protected function beforeAction($action)
{
    if (isset($_GET['type'])) {
        $_GET['type'] = 'foo';
    }
    ...
    return parent::beforeAction($action);
}

public function actionTest($type)
{
   # $type === 'foo'
   ...
}

更改 beforeAction 中的操作以满足您的任何要求。