SilverStripe 3.5.1 - 如果用户尝试导航到 amp.html 版本,则重定向到 SilverStripe 页面的 AbsoluteLink
SilverStripe 3.5.1 - Redirect to AbsoluteLink of SilverStripe page if user tries to navigate to amp.html version
我正在开发 SilverStripe AMP 模块 (https://github.com/thezenmonkey/silverstripe-amp) 的分支,如果 amp.html link还没有生成到页眉中
一些附加信息:我们添加了 2 个字段,它们将显示在每个页面的 AMP 版本上:AmpImage 和 AmpContent(富文本编辑器)。如果其中任何一个为空,我将代码设置为不为相关 SilverStripe 页面生成 AMP 页面。这似乎已经足够了,但增加了一个额外的要求,即提到的重定向功能,因此没有人可以实际导航到 amp.html 页面。
我正在考虑使用 AmpSiteTreeExtension 文件进行重定向,但它似乎不允许重定向,然后我想到了 Page.php 中的一个函数来检查 url 是否包含amp.html,然后用 AmpSiteTreeExtension 引用它,但每次尝试时,我都会收到一条错误消息,提示该函数在 "Page" 上不存在,或者它不是 public。
这种情况有什么好的处理方法吗?最好使用 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;
}
}
据我所知,您正试图在 SiteTree 扩展的 MetaTags()
方法中重定向...据我所知,该 MetaTags()
方法可能用于一些像这样的 Silverstripe 模板:$MetaTags
...而且您无法通过这种方式应用重定向。
您应该在控制器 class 中执行所有这些重定向操作,从您的示例来看,该控制器可能是 AmpController
class,它可能通过扩展 Page_Controller
class.
现在我假设 AmpController 是 Page_Controller 的扩展,所以我会这样做:
class Page_Controller extends ContentController {
public function init() {
parent::init();
// you might have some other stuff here
// make sure this is the last line in this method
$this->extend('updateInit');
}
public function yourRedirectMethod() {
// do your redirect thing here
}
}
关键如下:
我在控制器中扩展了 init()
方法 - 这将允许我
使用扩展页面控制器的 init()
功能
updateInit()
扩展中的方法 class (AmpController
在这个
案例).
而不是将执行重定向的方法添加到 Page
class,我把它加到Page_Controller
class(
yourRedirectMethod()
方法).
现在是 AmpController class,我在其中实现了 updateInit() 方法:
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;
}
public function updateInit() {
$should_redirect = true; // of course you add your own condition here to decide wether to redirect or not
if ($should_redirect) {
$this->owner->yourRedirectFunction();
}
}
}
这里唯一的事情是,您需要更新上面的 $should_redirect
变量(我已将此示例默认设置为 true - 但您可以在此处决定是否应该重定向或不是)...是的,您可以在 AmpController
class 中参考 Page
class 中的内容,我认为,例如:$this->owner->Title
我正在开发 SilverStripe AMP 模块 (https://github.com/thezenmonkey/silverstripe-amp) 的分支,如果 amp.html link还没有生成到页眉中
一些附加信息:我们添加了 2 个字段,它们将显示在每个页面的 AMP 版本上:AmpImage 和 AmpContent(富文本编辑器)。如果其中任何一个为空,我将代码设置为不为相关 SilverStripe 页面生成 AMP 页面。这似乎已经足够了,但增加了一个额外的要求,即提到的重定向功能,因此没有人可以实际导航到 amp.html 页面。
我正在考虑使用 AmpSiteTreeExtension 文件进行重定向,但它似乎不允许重定向,然后我想到了 Page.php 中的一个函数来检查 url 是否包含amp.html,然后用 AmpSiteTreeExtension 引用它,但每次尝试时,我都会收到一条错误消息,提示该函数在 "Page" 上不存在,或者它不是 public。
这种情况有什么好的处理方法吗?最好使用 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;
}
}
据我所知,您正试图在 SiteTree 扩展的 MetaTags()
方法中重定向...据我所知,该 MetaTags()
方法可能用于一些像这样的 Silverstripe 模板:$MetaTags
...而且您无法通过这种方式应用重定向。
您应该在控制器 class 中执行所有这些重定向操作,从您的示例来看,该控制器可能是 AmpController
class,它可能通过扩展 Page_Controller
class.
现在我假设 AmpController 是 Page_Controller 的扩展,所以我会这样做:
class Page_Controller extends ContentController {
public function init() {
parent::init();
// you might have some other stuff here
// make sure this is the last line in this method
$this->extend('updateInit');
}
public function yourRedirectMethod() {
// do your redirect thing here
}
}
关键如下:
我在控制器中扩展了
init()
方法 - 这将允许我 使用扩展页面控制器的init()
功能updateInit()
扩展中的方法 class (AmpController
在这个 案例).而不是将执行重定向的方法添加到
Page
class,我把它加到Page_Controller
class(yourRedirectMethod()
方法).
现在是 AmpController class,我在其中实现了 updateInit() 方法:
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;
}
public function updateInit() {
$should_redirect = true; // of course you add your own condition here to decide wether to redirect or not
if ($should_redirect) {
$this->owner->yourRedirectFunction();
}
}
}
这里唯一的事情是,您需要更新上面的 $should_redirect
变量(我已将此示例默认设置为 true - 但您可以在此处决定是否应该重定向或不是)...是的,您可以在 AmpController
class 中参考 Page
class 中的内容,我认为,例如:$this->owner->Title