简体   繁体   English

Symfony的动态登出目标

[英]Dynamic Logout Target For Symfony


I have implemented a basic authentication system using symfony framework. 我已经使用symfony框架实现了基本的身份验证系统。 It is working as expected. 它按预期工作。 But I would like to have one more feature. 但我想再增加一个功能。

After logout , I should be redirected to previous location. logout ,应将我重定向到先前的位置。 I followed following question for implementing this feature. 我遵循以下问题来实现此功能。
Symfony2 Dynamic Logout Target? Symfony2动态注销目标?

But it is giving me one error message. 但这给了我一个错误信息。

Unable to generate a URL for the named route "dynamic_route_name" as such route does not exist. 无法为命名路由“ dynamic_route_name”生成URL,因为这样的路由不存在。

Any Idea, why this is happening? 任何想法,为什么会这样?

I assume you have used the following code from the example post 我假设您已使用示例帖子中的以下代码

return new RedirectResponse($this->container->get('router')->generate('dynamic_route_name'));

This expects the route dynamic_route_name to exist, which wouldn't really take you to the previous page, but to a static route (where you can do all sorts of tricks in the controller if you want to) 这希望路由dynamic_route_name存在,这实际上并不会带您到上一页,而是会到达静态路由(如果需要,您可以在其中执行各种技巧)

Instead you can rather try the second answer and use 相反,您可以尝试第二个答案并使用

$target_url = $request->query->get('target_url')
                  ? $request->query->get('target_url')
                  : "/";
    return new RedirectResponse($target_url);

Where target_url is the route you want to redirect to (set in the template to the current route) or any other logic you can use to determine which page to go to 其中target_url是您要重定向到的路由(在模板中设置为当前路由)或可用于确定要转到哪个页面的任何其他逻辑

To redirect to current page after logout, you may set logout link as follows: 要在注销后重定向到当前页面,可以如下设置注销链接:

<a href="{{ path('logout', {'target_url': app.request.uri})  }}">Logout</a>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM