SilverStripe 3.5.1 - 如果用户尝试导航到amp.html版本,则重定向到SilverStripe页面的AbsoluteLink

问题描述 投票:0回答:1

我正在研究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

AmpController

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;
    }
}
silverstripe google-amp
1个回答
1
投票

从我所知道的,你正试图重新定位SiteTree扩展的MetaTags()方法...而且从我所知道的,MetaTags()方法它可能在一些Silverstripe模板中使用,如下所示:$MetaTags

......并且你无法以这种方式应用重定向。

你应该在控制器类中做所有这些重定向的东西,从你的控制器的例子来看,它可能是AmpController类,它是probalby扩展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

© www.soinside.com 2019 - 2024. All rights reserved.