SilverStripe 3.5.1-如果用户尝试导航到amp.html版本,则重定向到SilverStripe页面的AbsoluteLink
我正在处理SilverStripe AMP模块()的一个分支,如果页面头部没有生成AMP.html链接,我必须阻止任何人导航到SilverStripe页面的AMP.html版本 另外一点信息:我们添加了两个字段,用于显示在每个页面的AMP版本上:AmpImage和AmpContent(富文本编辑器)。如果其中任何一个为空,我的代码设置为不为讨论中的SilverStripe页面生成AMP页面。这似乎已经足够了,但是增加了一个额外的要求,即上面提到的重定向功能,这样就没有人能够真正导航到amp.html页面 我想用AmpSiteTreeExtension文件进行重定向,但它似乎不允许重定向,然后我想在Page.php中使用一个函数来检查url是否包含amp.html,然后用AmpSiteTreeExtension引用它,但每次尝试时,我都会收到一个错误,说“Page”上不存在该函数或者它不是公开的SilverStripe 3.5.1-如果用户尝试导航到amp.html版本,则重定向到SilverStripe页面的AbsoluteLink,silverstripe,google-amp,Silverstripe,Google Amp,我正在处理SilverStripe AMP模块()的一个分支,如果页面头部没有生成AMP.html链接,我必须阻止任何人导航到SilverStripe页面的AMP.html版本 另外一点信息:我们添加了两个字段,用于显示在每个页面的AMP版本上:AmpImage和AmpContent(富文本编辑器)。如果其中任何一个为空,我的代码设置为不为讨论中的SilverStripe页面生成AMP页面。这似乎已经足够了,但是增加了一个额外的要求,即上面提到的重定向功能,这样就没有人能够真正导航到amp.ht
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
有没有处理这种情况的好办法?最好是使用Page.php还是使用其他方法
以下是我正在处理的文件:
安培数扩展
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
公共函数元标记(&$tags)
{
如果($this->owner->AmpContent!=“”&&&$this->owner->AmpImageID!=“”){
如果($this->owner->class!=“HomePage”){
$ampLink=$this->owner->AbsoluteLink()“amp.html”;
}否则{
$ampLink=$this->owner->AbsoluteLink()“home/”“amp.html”;
}
$tags.=“\n”;
}
//在这里添加重定向?引用Page.php中的函数,如:$this->owner->functionName()会导致上述错误
}
}
据我所知,您正试图在SiteTree扩展的MetaTags()
方法中重定向。。。据我所知,MetaTags()
方法它可能在一些Silverstripe模板中使用,比如:$MetaTags
…您无法以这种方式应用重定向
您应该在控制器类中执行所有这些重定向操作,从您的控制器示例来看,可能是AmpController
类扩展了Page\u controller
类的功能
现在我假设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
}
}
这里的关键点如下:
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();
}
}
}
我在控制器中扩展了init()
方法-这将允许我
使用扩展页控制器的init()
功能
扩展类(AmpController
中的updateInit()
方法
案例)
不要将执行重定向的方法添加到页面
类,我将其添加到页面_Controller
类(
yourRedirectMethod()
method)
现在是AmpController类,在这里我实现了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();
}
}
}
类控制器扩展扩展{
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
私有静态$allowed_actions=array('amp');
私有静态$url\u处理程序=数组(
“amp.html”=>“amp”
);
公共职能
{
要求::clear();
$class=Controller::curr()->ClassName;
$page=$this->owner->renderWith(数组(“$class”);
返回$this->AmplfyHTML($page);
}
公共函数AmplfyHTML($content)
{
如果(!$content){
返回false;
}
$content=preg\u replace('/style=\\“[^\\”]*\\“/”,''$content);
$content=str_replace(“如果有帮助,请告诉我:)另外,现在刚刚看到了这一点…您可能正在使用AmpSiteTreeExtension扩展SiteTree,我不奇怪您会出现这种错误…因为这些扩展在Silverstripe中的工作方式,以及您设置的方式,AmpSiteTreeExtension只看到SiteTree中的内容,而没有看到Page类中的内容…总之,我上面提出的方法,应该可以工作。您看到的大部分内容都是模块本身的一部分。我没有编写大部分代码。我只是在$MetaTags函数中编写了if语句。这是为了防止在两个字段中的一个为空时将amp.html url放在SS标头中。我尝试了您的代码设置,但它似乎可以工作。因此,谢谢:)
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