【问题标题】:Symfony routing: how to redirect from UUID to custom slugSymfony 路由:如何从 UUID 重定向到自定义 slug
【发布时间】:2016-11-03 13:26:19
【问题描述】:

我有一个可以使用http://example.com/company/d9c363ae-a1b7-11e6-a66d-9e9923e30d94/http://example.com/company/custom-domain.com 访问的实体。

现在,Company 可以有 slug,但也可以没有。

所以,我想检查它是否设置了域,如果有,并且 URL 是否基于 UUID,我想将其重定向到带有 slug 的版本。如果它没有设置域,我只是使用 UUID 显示页面。

我在this Symfony's tutorial:

# routing.yml

redirect_company_to_domain:
    path: /company/{domain_host_or_id}
    defaults: { _controller: AppBundle:Company:redirectToDomain }
    requirements:
        url: .*/$
    methods: [GET]

在我的控制器中我写了这个方法:

/**
 * Redirect the URLs with a trailing slash to the version without it.
 *
 * @param Request $request
 *
 * @return \Symfony\Component\HttpFoundation\RedirectResponse
 */
public function redirectToDomainAction(Company $company, Request $request)
{
    die(dump($company));
}

问题是控制器永远不会被调用。

我错过了什么或做错了什么?为什么路径永远不会被路由拦截?

可能是因为我在routing.yml 中混合了注释和配置?因为CompanyController的路由都是用注解设置的。

但是使用app/console debug:router我可以正确看到路由集:

redirect_company_to_domain             GET        ANY      ANY    /company/{domain_host_or_id}

此外,我确信本教程中描述的方法可以正常工作,因为我已经实现了它以删除尾部斜杠。

我不明白为什么这个重定向不起作用。

【问题讨论】:

    标签: php symfony redirect


    【解决方案1】:

    我认为是因为您指定的参数(在括号中)不正确。

    你可以试试这个吗:

    redirect_company_to_domain:
        path: /company/{domain_host_or_id}
        defaults: { _controller: AppBundle:Company:redirectToDomain }
        requirements:
            domain_host_or_id: .*/$
        methods: [GET]
    

    我认为这是问题所在,但我不能 100% 确定。你可以试一试并告诉我们吗?

    【讨论】:

      猜你喜欢
      • 2017-06-02
      • 2016-05-17
      • 2018-09-01
      • 2016-07-28
      • 2020-05-09
      • 1970-01-01
      • 2017-03-18
      • 2015-05-15
      • 2011-05-12
      相关资源
      最近更新 更多