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



当您访问/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>

在你自己的 routeting.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扩展 FOSUserBundleProfileController。这就是为什么我也将控制器更改为AppBundle:Profile:edit,但显然这需要匹配您的代码。

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

public function showAction(Request $request, $id)

另请参阅此处以获取更详细的答案(对于其他路由(:如何自定义 FOS 用户捆绑 URL

最新更新