如何在 Silverstripe 中对 $AbsoluteLink 使用不同的协议

How to use a different protocol with $AbsoluteLink in Silverstripe

Silverstripe 模板 语法中 $AbsoluteLink returns page/object 的完整 URL,包括协议和主机:

http://www.example.com/event/ics

我希望能够使用不同的协议调用完整的 URL:

webcal://www.example.com/event/ics

实现此目标的最佳方法是什么?

在您的页面上创建一个新的 getter 功能:

public function WebcalLink() {
    $absolute = $this->AbsoluteLink();
    $webcal = str_replace(Director::protocol(), "webcal://", $absolute);
    return $webcal;
}

您可以使用 $WebcalLink

从您的模板中调用它

定义一个自定义 link 方法,用您想要的协议替换当前的网站协议。即

public function WebCalLink()
{
    return str_replace(Director::protocol(), 'webcal://', Director::protocolAndHost()) . $this->Link();
}