【发布时间】:2016-09-09 08:43:06
【问题描述】:
我正在使用yii\helpers\Url 帮助程序在菜单中访问我网站的网址。但是应该有一个 url 去另一个站点,比如www.anothersite.com/action。
如何通过yii\helpers\Url helpers 放置此链接?
是否可以使用 yii2 yii\helpers\Url helpers 添加到其他站点的 url?
【问题讨论】:
我正在使用yii\helpers\Url 帮助程序在菜单中访问我网站的网址。但是应该有一个 url 去另一个站点,比如www.anothersite.com/action。
如何通过yii\helpers\Url helpers 放置此链接?
是否可以使用 yii2 yii\helpers\Url helpers 添加到其他站点的 url?
【问题讨论】:
这个灵魂是可重复使用的,所以试一试。
在应用程序的配置文件中定义一个别名。
Yii::setAlias('@another', 'http://anothersite.com/action');
然后您就可以在整个应用程序中使用它,例如,
//assuming you've already imported yii\helpers\Url
Url::to('@another');
【讨论】:
是的,就像这样:
<?= \yii\helpers\Url::to('http://www.anothersite.com/action') ?>
但这与以下内容相同:
<?= 'http://www.anothersite.com/action' ?>
如果你想在那里为另一个网站使用路由(例如,你可以使用带有动作参数的数组路由)你需要先为另一个网站配置 urlManager,否则它将不起作用。
【讨论】:
mysite.com/http://www.anothersite.com/action 。而且我不想使用路线。只是给定网址。
Url::toRoute 或Url::to 与数组而不是字符串。