【问题标题】:Yii2 yii\helpers\Url helpers Url to another siteYii2 yii\helpers\Url helpers Url 到另一个站点
【发布时间】:2016-09-09 08:43:06
【问题描述】:

我正在使用yii\helpers\Url 帮助程序在菜单中访问我网站的网址。但是应该有一个 url 去另一个站点,比如www.anothersite.com/action

如何通过yii\helpers\Url helpers 放置此链接?

是否可以使用 yii2 yii\helpers\Url helpers 添加到其他站点的 url?

【问题讨论】:

    标签: php url yii yii2


    【解决方案1】:

    这个灵魂是可重复使用的,所以试一试。

    在应用程序的配置文件中定义一个别名。

    Yii::setAlias('@another', 'http://anothersite.com/action');
    

    然后您就可以在整个应用程序中使用它,例如,

    //assuming you've already imported yii\helpers\Url
    Url::to('@another'); 
    

    【讨论】:

      【解决方案2】:

      是的,就像这样:

      <?= \yii\helpers\Url::to('http://www.anothersite.com/action') ?>
      

      但这与以下内容相同:

      <?= 'http://www.anothersite.com/action' ?>
      

      如果你想在那里为另一个网站使用路由(例如,你可以使用带有动作参数的数组路由)你需要先为另一个网站配置 urlManager,否则它将不起作用。

      【讨论】:

      • 谢谢你的回答,我一周前试过了,但它显示 mysite.com/http://www.anothersite.com/action 。而且我不想使用路线。只是给定网址。
      • 如何调用它?
      • 我打赌你使用Url::toRouteUrl::to 与数组而不是字符串。
      • 嘿@Bizley,我会尽快测试它并通知你确切的结果。谢谢!
      猜你喜欢
      • 2023-03-11
      • 1970-01-01
      • 2011-07-26
      • 2014-05-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-04
      相关资源
      最近更新 更多