How to use a different protocol with $AbsoluteLink in Silverstripe
Asked Answered
C

2

5

In the Silverstripe templating syntax $AbsoluteLink returns the full URL of a page/object, including the protocol and host:

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

I want to be able to call a full URL with a different protocol:

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

What is the best way to achieve this?

Colpin answered 9/6, 2016 at 0:1 Comment(0)
M
5

Define a custom link method that replaces the current website protocol with your desired one. ie.

public function WebCalLink()
{
    return str_replace(Director::protocol(), 'webcal://', Director::protocolAndHost()) . $this->Link();
}
Much answered 9/6, 2016 at 0:24 Comment(2)
Isn't $this->Link() is the relative link? so str_replace will not work in this case?Wamble
The str_replace is not operating on $this->Link(), it's operating on Director::protocolAndHost()Much
M
5

Make a new getter function on your page:

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

You can call it from your template using $WebcalLink

Miserly answered 9/6, 2016 at 0:22 Comment(0)
M
5

Define a custom link method that replaces the current website protocol with your desired one. ie.

public function WebCalLink()
{
    return str_replace(Director::protocol(), 'webcal://', Director::protocolAndHost()) . $this->Link();
}
Much answered 9/6, 2016 at 0:24 Comment(2)
Isn't $this->Link() is the relative link? so str_replace will not work in this case?Wamble
The str_replace is not operating on $this->Link(), it's operating on Director::protocolAndHost()Much

© 2022 - 2024 — McMap. All rights reserved.