FosUserBundle:如何将默认 /profile 路径覆盖为自定义路径?

FosUserBundle: How to override the default /profile path to custom path?

当您访问 /profile 路径时,它会将您带到默认的个人资料页面。

我进行了更改,因此当您访问个人资料时,您需要向 url 添加一个 ID 才能访问正确的页面。

但是,/profile 仍然转到默认页面,如何禁用此路由或使其转到自定义路径?

提前致谢。

如果您查看 @FOSUserBundle/Resources/config/routing/profile.xml,您将看到以下路线。您可以在自己的路由中使用相同的名称来覆盖任何路由(或其他配置)

<route id="fos_user_profile_show" path="/" methods="GET">
    <default key="_controller">FOSUserBundle:Profile:show</default>
</route>

<route id="fos_user_profile_edit" path="/edit" methods="GET POST">
    <default key="_controller">FOSUserBundle:Profile:edit</default>
</route>

在您自己的 routing.yml 中,您只需像这样覆盖:

fos_user_profile_show:
    path: /profile/{id}
    defaults: { _controller: AppBundle:Profile:show }

fos_user_profile_edit:
    path: /profile/edit/{id}
    defaults: { _controller: AppBundle:Profile:edit }

请注意,您很可能不再使用默认 ProfileController,而是使用自己的 ProfileController 扩展 FOSUserBundle ProfileController。这就是为什么我也将控制器更改为 AppBundle:Profile:edit,但显然这需要与您的代码匹配。

另请注意,{id} 需要在您的代码中实现,例如:

public function showAction(Request $request, $id)

另请参阅此处以获得更详细的答案(对于另一条路线):How to customize FOS UserBundle URLs