Silverstripe 将外部 URL 呈现为相关链接
Silverstripe Rendering External URLs as Relative Links
我遇到 SilverStripe 将外部 URLs 视为相对 links 的问题。
我有一个数据对象:
class Artist extends DataObject {
private static $db = array(
'Title' => 'Varchar(255)',
'Content' => 'HTMLText',
'Website' => 'Varchar(255)',
);
}
艺术家网站通过 <a href="$Website" target="_blank">
呈现。问题是 URL 被附加到网站的基础 URL,所以我们最终得到类似:
<a href="mysite.com/www.artistsite.com" target="_blank">
而不是所需的:
<a href="www.artistsite.com" target="_blank">
但是,如果 $Website 包含协议(http 或 https),则 link 会按预期工作。所以如果 $Website 是 http://www.artistsite.com 那么我们得到:
<a href="http://www.artistsite.com" target="_blank">
此站点包含数百个,最终是数千个客户维护的艺术家记录。理想情况下,客户端将能够粘贴 URLs 而不必担心将 http 或 https 附加到每个。
有人有什么想法吗?这与 SilverStripe forums 中描述的问题相同,但尚未发布解决方案。
此站点在 SilverStripe 3.6 上。
这不是 SilverStripe 的直接问题。
给定一个 html 文档,其中包含以下内容:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<a href="google.com" target="_blank">test</a>
<a href="www.google.com" target="_blank">test</a>
</body>
</html>
所有这些都作为站点的相对链接打开,而不是外部 url。
见
帮助管理员粘贴功能链接是添加一个 onBeforeWrite 来测试 url 是否包含有效协议,如果不包含,至少自动添加 http://。或者使用@wmk 在评论中建议的自动执行的模块。
由于记录已经建立并且我不想将所有网站字段转换为不同的字段类型,我选择根据@olli-tyynelä 和@bummzack 的建议添加一个 onBeforeWrite:
public function onBeforeWrite() {
$url = $this->Website;
if ( $ret = parse_url($url) ) {
if ( !isset($ret["scheme"]) ) {
$url = "http://{$url}";
$this->Website = $url;
$this->write();
}
}
parent::onBeforeWrite();
}
谢谢:)
我遇到 SilverStripe 将外部 URLs 视为相对 links 的问题。
我有一个数据对象:
class Artist extends DataObject {
private static $db = array(
'Title' => 'Varchar(255)',
'Content' => 'HTMLText',
'Website' => 'Varchar(255)',
);
}
艺术家网站通过 <a href="$Website" target="_blank">
呈现。问题是 URL 被附加到网站的基础 URL,所以我们最终得到类似:
<a href="mysite.com/www.artistsite.com" target="_blank">
而不是所需的:
<a href="www.artistsite.com" target="_blank">
但是,如果 $Website 包含协议(http 或 https),则 link 会按预期工作。所以如果 $Website 是 http://www.artistsite.com 那么我们得到:
<a href="http://www.artistsite.com" target="_blank">
此站点包含数百个,最终是数千个客户维护的艺术家记录。理想情况下,客户端将能够粘贴 URLs 而不必担心将 http 或 https 附加到每个。
有人有什么想法吗?这与 SilverStripe forums 中描述的问题相同,但尚未发布解决方案。
此站点在 SilverStripe 3.6 上。
这不是 SilverStripe 的直接问题。
给定一个 html 文档,其中包含以下内容:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<a href="google.com" target="_blank">test</a>
<a href="www.google.com" target="_blank">test</a>
</body>
</html>
所有这些都作为站点的相对链接打开,而不是外部 url。
见
帮助管理员粘贴功能链接是添加一个 onBeforeWrite 来测试 url 是否包含有效协议,如果不包含,至少自动添加 http://。或者使用@wmk 在评论中建议的自动执行的模块。
由于记录已经建立并且我不想将所有网站字段转换为不同的字段类型,我选择根据@olli-tyynelä 和@bummzack 的建议添加一个 onBeforeWrite:
public function onBeforeWrite() {
$url = $this->Website;
if ( $ret = parse_url($url) ) {
if ( !isset($ret["scheme"]) ) {
$url = "http://{$url}";
$this->Website = $url;
$this->write();
}
}
parent::onBeforeWrite();
}
谢谢:)