我正在开发 SilverStripe AMP 模块 ( https://github.com/thezenmonkey/silverstripe-amp ) 的分支,如果 amp.html 链接有,则必须阻止任何人导航到 SilverStripe 页面的 amp.html 版本没有被生成到页面的头部。
一些附加信息:我们添加了 2 个字段,这些字段旨在显示在每个页面的 AMP 版本上:AmpImage 和 AmpContent(富文本编辑器)。如果其中任何一个为空,我将代码设置为不为相关 SilverStripe 页面生成 AMP 页面。这似乎已经足够了,但添加了一个额外的要求,即提到的重定向功能,因此没有人可以真正导航到 amp.html 页面。
我正在考虑使用 AmpSiteTreeExtension 文件进行重定向,但它似乎不允许重定向,然后我想在 Page.php 中有一个函数来检查 url 是否包含 amp.html,然后使用 AmpSiteTreeExtension 引用它,但是每次我尝试时,我都会收到一条错误消息,指出该功能在“页面”上不存在或者它不是公开的。
有没有好的方法来处理这种情况?最好使用 Page.php 还是使用其他方法?
以下是我正在使用的文件:
AmpSiteTreeExtension
public function MetaTags(&$tags)
{
if ($this->owner->AmpContent != "" && $this->owner->AmpImageID != "") {
if ($this->owner->class != "HomePage") {
$ampLink = $this->owner->AbsoluteLink() . "amp.html";
} else {
$ampLink = $this->owner->AbsoluteLink() . "home/" . "amp.html";
}
$tags .= "<link rel='amphtml' href='$ampLink' /> \n";
}
//add a redirect here? Referencing a function from Page.php like so: $this->owner->functionName() causes the error mentioned above
}
}
<?php
放大器控制器
class AmpController extends Extension
{
private static $allowed_actions = array('amp');
private static $url_handlers = array(
'amp.html' => 'amp'
);
public function amp()
{
Requirements::clear();
$class = Controller::curr()->ClassName;
$page = $this->owner->renderWith(array("$class"."_amp", "Amp"));
return $this->AmplfyHTML($page);
}
public function AmplfyHTML($content)
{
if (!$content) {
return false;
}
$content = preg_replace('/style=\\"[^\\"]*\\"/', '', $content);
$content = str_replace("<img", "<amp-img", $content);
return $content;
}
}