如何检查用户是否已登录 Symfony?

编程入门 行业动态 更新时间:2024-10-27 23:21:07
本文介绍了如何检查用户是否已登录 Symfony?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我收到了这张表格:

{{ form_start(form) }} <input type="submit" class="form_submit_button" name="register_interest" value="Add to favourites"/> {{ form_end(form) }}

我想检查用户是否登录,我这样做了:

I want to check if user is logged in and I did this :

{% if is_granted('IS_AUTHENTICATED_FULLY') %}

如果用户未登录,它会隐藏按钮.

And it hides the button if the user is not logged in.

即使我没有登录,我也希望该按钮显示,并在提交表单后检查用户是否登录.

I want that button to show even if I am not logged in and after the form submit to check if the user is logged in .

我该怎么做?

谢谢!

推荐答案

必须在您的 php 控制器中执行检查.此外,您还可以执行 javascript 检查,以防止在用户未登录时提交表单.

The check must be performed in your php controller. Additionally, you can also perform a javascript check which prevents submitting the form if user is not logged in.

要检查控制器中的用户登录:

To check for user login in your controller:

if ($this->isGranted('ROLE_USER') == false) { // show some error message, throw exception etc... }

另一种方法:

$this->denyAccessUnlessGranted('ROLE_USER');

要检查客户端 (javascript),您可以执行以下操作:

To check on the client side (javascript), you could do something like this:

{% if not is_granted('IS_AUTHENTICATED_REMEMBERED') %} <script> // wrap into document.ready() if needed $('#your_submit_button_id').on('click', function(e) { e.preventDefault(); alert('You must be logged in to use this function'); }); </script> {% endif %}

更多推荐

如何检查用户是否已登录 Symfony?

本文发布于:2023-10-11 17:39:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1482376.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:用户   Symfony

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!