在 yii2 中创建动态 url

create dynamic url in yii2

我的规则配置是:

[
    'pattern' => 'admin/post',
    'route' => 'admin/default/post',
],

成功了!当打开 http://example.com/admin/post 显示 admin/default/post 给我时,

但是当我想使用此代码创建动态 url 时:

echo \yii\helpers\Url::to('/admin/default/test');

输出为http://example.com/admin/default/test

谁来创建动态 url 正确?

关于Url::to()

This method is very similar to [[toRoute()]]. The only difference is that this method requires a route to be specified as an array only. If a string is given, it will be treated as a URL.

您需要传递 array 而不是字符串

\yii\helpers\Url::to(['/admin/default/test']);