yiisoft/yii2-twig

Use the method in the template error

mro1127 opened this issue · 3 comments

我的配置

'view' => [
            'class' => 'yii\web\View',
            'renderers' => [
                'twig' => [
                    'class' => 'yii\twig\ViewRenderer',
                    'cachePath' => '@runtime/Twig/cache',
                    // Array of twig options:
                    'options' => [
                        'auto_reload' => true,
                    ],
                    'globals' => [
                        'Url' => ['class' => '\yii\helpers\Url'],
                        'html' => ['class' => '\yii\helpers\Html'],
                    ],
                ],
            ],

在twig模板使用Url的方法时,
{{ Url.to(['site/home']) }}
报错:

Twig_Error_Runtime
An exception has been thrown during the rendering of a template ("call_user_func_array() expects parameter 1 to be a valid callback, first array member is not a valid class name or object").
Caused by: yii\base\ErrorException
call_user_func_array() expects parameter 1 to be a valid callback, first array member is not a valid class name or object
in /var/www/yii-admin/vendor/yiisoft/yii2-twig/ViewRendererStaticClassProxy.php at line 60

查了一下,是因为传入call_user_func_array的参数为: [['class' => '\yii\helpers\Url' ], 'base']

我修改了:vendor\yiisoft\yii2-twig\ViewRenderer.php 第272行,
$value = new ViewRendererStaticClassProxy($value);
改为
$value = new ViewRendererStaticClassProxy($value['class']);

就可以了。
如果修改globals为以下配置也可以实现

        'globals' => [
                        'Url' => '\yii\helpers\Url',
                        'html' => '\yii\helpers\Html',
                    ],

是不是修改配置比较好?

Translation


My configuration

'view' => [
            'class' => 'yii\web\View',
            'renderers' => [
                'twig' => [
                    'class' => 'yii\twig\ViewRenderer',
                    'cachePath' => '@runtime/Twig/cache',
                    // Array of twig options:
                    'options' => [
                        'auto_reload' => true,
                    ],
                    'globals' => [
                        'Url' => ['class' => '\yii\helpers\Url'],
                        'html' => ['class' => '\yii\helpers\Html'],
                    ],
                ],
            ],

When using the Url method in a twig template,

{{ Url.to(['site/home']) }}

Error:

Twig_Error_Runtime
An exception has been thrown during the rendering of a template ("call_user_func_array() expects parameter 1 to be a valid callback, first array member is not a valid class name or object").
Caused by: yii\base\ErrorException
call_user_func_array() expects parameter 1 to be a valid callback, first array member is not a valid class name or object
in /var/www/yii-admin/vendor/yiisoft/yii2-twig/ViewRendererStaticClassProxy.php at line 60

Checked because the arguments passed to call_user_func_array are: [['class' => '\ yii \ helpers \ Url'], 'base']

I modified: vendor \ yiisoft \ yii2-twig \ ViewRenderer.php line 272,

$value = new ViewRendererStaticClassProxy($value);

Change to

$value = new ViewRendererStaticClassProxy($value['class']);

It's fine
If you modify the globals for the following configuration can be achieved

        'globals' => [
                        'Url' => '\yii\helpers\Url',
                        'html' => '\yii\helpers\Html',
                    ],

Is it right? Modify the configuration is better?

@samdark Thinks!!

Seems to be already done.