【问题标题】:Drupal 8 Url Alter with processOutbound and preg_replaceDrupal 8 Url Alter with processOutbound 和 preg_replace
【发布时间】:2017-10-11 10:44:57
【问题描述】:

我正在开发一个 Drupal 网站,我需要将所有包含“member”的 URL 更改为“follower”。

例如:

  • www.site.com/member ====> www.site.com/follower
  • www.site.com/members ====> www.site.com/followers
  • www.site.com/members/1/info ====> www.site.com/followers/1/info
  • www.site.com/something/member ====> www.site.com/something/follower

等等

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

但它也不起作用。各位大神能帮我解决一下吗?

请在下面找到我的课程代码。

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$1', $path);
  }
}

【问题讨论】:

    标签: php url drupal drupal-8 alter


    【解决方案1】:

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

    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;
      }
    
    }
    

    谢谢大家

    【讨论】:

      猜你喜欢
      • 2020-07-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-11
      • 1970-01-01
      • 2023-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多