Yii2: call runAction with params in controller get a exception.

Created on 4 Aug 2016  路  2Comments  路  Source: yiisoft/yii2

What steps will reproduce the problem?

in home page, I use runAction to call other action with params. but in another action, I can not get the params. the home page code like this:

public function actionIndex()
    {
        try {
            $data = Yii::$app->runAction('/v1/user/list',['params'=>['uid'=>'123']]);
            $result = [
                'status' => 'success',
                'code' => '0',
                'data' => $data
            ];
        }catch (\Exception $e){
            $result = [
                'status' => 'success',
                'code' => '0',
                'data' => $e->getMessage()
            ];
        }
        return $result;
    }

another action in modules v1, and controller named UserController. the code like this:

public function actionList($params)
    {
        print_r($params);
        $result = array_map(function($record) {
            return $record->attributes;
        },User::find()->all());
        return $result;
    }

What is the expected result?

I want get result:"Array('uid'=>'123')";

What do you get instead?

I get the exception: "Invalid data received for parameter "params"."

Additional info

I find the error from the file:
yii2\vendor\yiisoft\yii2\web\Controller.php
the function code:

public function bindActionParams($action, $params)
    {
        if ($action instanceof InlineAction) {
            $method = new \ReflectionMethod($this, $action->actionMethod);
        } else {
            $method = new \ReflectionMethod($action, 'run');
        }

        $args = [];
        $missing = [];
        $actionParams = [];

        foreach ($method->getParameters() as $param) {
            $name = $param->getName();
            if (array_key_exists($name, $params)) {
                if ($param->isArray()) {
                    $args[] = $actionParams[$name] = (array) $params[$name];
                **} elseif (!is_array($params[$name])) {**
                    $args[] = $actionParams[$name] = $params[$name];
                } else {
                    throw new BadRequestHttpException(Yii::t('yii', 'Invalid data received for parameter "{param}".', [
                        'param' => $name,
                    ]));
                }
                unset($params[$name]);
            } elseif ($param->isDefaultValueAvailable()) {
                $args[] = $actionParams[$name] = $param->getDefaultValue();
            } else {
                $missing[] = $name;
            }
        }

        if (!empty($missing)) {
            throw new BadRequestHttpException(Yii::t('yii', 'Missing required parameters: {params}', [
                'params' => implode(', ', $missing),
            ]));
        }

        $this->actionParams = $actionParams;

        return $args;
    }

change the code "} elseif (!is_array($params[$name])) {" to "} elseif (is_array($params[$name])) {"
then go well.
| Q | A
| ---------------- | ---
| Yii version | 2.0.8
| PHP version | 5.6
| Operating system | xampp in windows

Most helpful comment

try this

public function actionList(array $params)
{


All 2 comments

try this

public function actionList(array $params)
{


yes, you are right. thx!

Was this page helpful?
0 / 5 - 0 ratings