<?php

namespace SamIT\Yii2\Traits;

use yii\base\InlineAction;
use yii\base\InvalidConfigException;
use yii\web\BadRequestHttpException;

/**
 * Trait that implements dependency injection for controller actions.
 * It should be bound only to subclasses of controller.
 */
trait ActionInjectionTrait
{
    /**
     * @see https://github.com/yiisoft/yii2/issues/9476
     * @inheritdoc
     */
    public function bindActionParams($action, $params)
    {

        if ($action instanceof InlineAction) {
            $callable = [$this, $action->actionMethod];
        } else {
            $callable = [$action, 'run'];
        }

        $actionParams = [];
        try {
            $args = \Yii::$container->resolveCallableDependencies($callable, $params);
        } catch (InvalidConfigException $e) {
            throw new BadRequestHttpException($e->getMessage());
        }

        foreach ((new \ReflectionMethod($callable[0], $callable[1]))->getParameters() as $i => $param) {
            $actionParams[$param->getName()] = $args[$i];
        }

        if (property_exists($this, 'actionParams')) {
            $this->actionParams = $actionParams;
        }
        
        return $args;
    }
}
<?php

namespace SamIT\Yii2\Traits;

use yii\base\InlineAction;
use yii\base\InvalidConfigException;
use yii\web\BadRequestHttpException;

/**
 * Trait that implements dependency injection for controller actions.
 * It should be bound only to subclasses of controller.
 */
trait ActionInjectionTrait
{
    /**
     * @see https://github.com/yiisoft/yii2/issues/9476
     * @inheritdoc
     */
    public function bindActionParams($action, $params)
    {

        if ($action instanceof InlineAction) {
            $callable = [$this, $action->actionMethod];
        } else {
            $callable = [$action, 'run'];
        }

        $actionParams = [];
        try {
            $args = \Yii::$container->resolveCallableDependencies($callable, $params);
        } catch (InvalidConfigException $e) {
            throw new BadRequestHttpException($e->getMessage());
        }

        foreach ((new \ReflectionMethod($callable[0], $callable[1]))->getParameters() as $i => $param) {
            $actionParams[$param->getName()] = $args[$i];
        }

        if (property_exists($this, 'actionParams')) {
            $this->actionParams = $actionParams;
        }
        
        // Dont put injected  params in requestedParams, this breaks the debugger.
        foreach($actionParams as $key => $value) {
            if (is_object($value)) {
                if (\Yii::$app->has($key, true)
                    && $value === \Yii::$app->get($key)
                ) {
                    $value = "Application component: $key";
                } else {
                    $value = "DI: " .get_class($value);
                }
            }
            \Yii::$app->requestedParams[$key] = $value;
        }
        return $args;
    }
}

Fix issue when debugger is used with DI
Fix issue when debugger is used with DI

This fix allows the debug module to properly serialize action params even if they are complex. It realizes this by replacing the params with string descriptions.