0

我正在开发 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;
    }
}
4

1 回答 1

1

据我所知,您正在尝试在MetaTags()SiteTree 扩展的方法中重定向……据我所知,该MetaTags()方法可能在某些 Silverstripe 模板中使用,如下所示:$MetaTags

...而且您无法以这种方式应用重定向。

您应该在控制器类中执行所有这些重定向内容,并且从您的示例中,该控制器可能是可能AmpController扩展该类功能的Page_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
  }

}

这里的关键是:

  1. init()在控制器中扩展了方法——这将允许我使用扩展类中的方法来扩展页面控制器的init()功能 updateInit()AmpController在本例中)。

  2. 我没有将执行重定向的方法添加到Page 类中,而是将其添加到Page_Controller类( yourRedirectMethod()方法)中。

现在来了 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();
      }
    }

}

这里唯一的事情是你需要更新$should_redirect上面的变量(我在这个例子中默认将它设置为 true - 但这里是你决定是否应该重定向的地方)......是的,你可以在我认为的课程中参考这里的AmpController课程内容,例如:Page$this->owner->Title

于 2018-03-02T18:43:53.957 回答