Drupal 8 Url 改变 processOutbound 和 preg_replace

Drupal 8 Url Alter with processOutbound and preg_replace

我正在一个 Drupal 网站上工作,我需要将包含 "member" 的所有 URL 更改为 "follower"。

例如:

等等

我尝试了几个没有用的东西,然后我发现 processOutbound 似乎是在我所有的 URL 中用 "follower" 替换 "member" 的正确方法。

但是也不行。你们能帮我解决这个问题吗?

请在下面找到我的 class 的代码。

class SquarePathProcessor implements InboundPathProcessorInterface, OutboundPathProcessorInterface {

  public function processInbound($path, Request $request) {

    return $path;
  }

  public function processOutbound($path, &$options = array(), Request $request = NULL, BubbleableMetadata $bubbleable_metadata = NULL) {
    return preg_replace('@^/member(.*)@', '/follower', $path);
  }
}

我做到了!!!这是解决方案:

class SquarePathProcessor implements InboundPathProcessorInterface, OutboundPathProcessorInterface {

  public function processInbound($path, Request $request) {
    if (strpos($path, '/follower') === 0) {
      $path = preg_replace('#^/follower#', '/member', $path);
    }
    return $path;
  }

  public function processOutbound($path, &$options = array(), Request $request = NULL, BubbleableMetadata $bubbleable_metadata = NULL) {
    if (strpos($path, '/member') === 0) {
      $path = preg_replace('#^/member#', '/follower', $path);
    }
    return $path;
  }

}

谢谢大家