我需要仅使用HTML DOM将所有相对URL替换为网页的绝对URL。可能吗?
我也在使用yii2。
实施例
<link href="/css/site.css" rel="stylesheet">
<a href="/page">Page</a>
结果:
<link href="http://www.example.com/css/site.css" rel="stylesheet">
<a href="http://www.example.com/page">Page</a>
答案 0 :(得分:2)
如果要链接到控制器操作,请将$ scheme参数设置为true以返回绝对URL。
Html::a( $text, $url = null, $options = [] )
http://www.yiiframework.com/doc-2.0/yii-helpers-baseurl.html#to()-detail
对于文件,您可以使用此// A normal string. This will use the exact string as the href attribute
$url = 'images/logo.png'; // This returns href='images/logo.png'
// A string starting with a Yii2 alias
$url = '@web/images/logo.png' // This returns href='http://www.example.com/images/logo.png'
以及以下选项
" "
答案 1 :(得分:0)
你是否使用网址助手
假设你在/ yii2 / web / css
中有css<?php
use yii\helpers\Url;
echo '<link href="' .Url::base() . '/css/site.css' .'" rel="stylesheet">';
假设你在/ yii2 / css中有css
echo '<link href="' .Url::base() . '../css/site.css' .'" rel="stylesheet">';