Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use a different protocol with $AbsoluteLink in Silverstripe

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?

like image 544
BaronGrivet Avatar asked Jun 09 '16 00:06

BaronGrivet


2 Answers

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();
}
like image 51
LiveSource Avatar answered Oct 07 '22 23:10

LiveSource


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

like image 23
cryptopay Avatar answered Oct 08 '22 00:10

cryptopay