I would like to create button in Twig to go back to whatever the previous page is and I found this code: "Go Back To Previous Page" Unfortunately tested it does not work in Symfony 4 default twig version - it will only return url of the current page. (source: https://stackoverflow.com/q....
How do we do this with Twig framework, is it possible?
Hmm, I don't know why it's not working, getting the referer from the request seems like the right way to do what you want, but just be aware of it might be null or a hacky user could modify it (of course, it would only affect himself). Probably what you want is "breadcrumbs" https://github.com/whiteoct...
I have successfully implemented "breadcrumbs". And in regard to Twig template's output expression {{ app.request.headers.get('referer') }} the reason it did not work was because had attribute rel="noopener noreferrer" in my anchor tag - it works now after removing "rel"
By the way, how can I handle ALREADY logged-in user if he goes to /login page? In my case he sees login form still but has logged-in status in web-profiler bar.
Good question, the default behavior is to redirect already logged-in users to the homepage (or any other page that makes more sense). So yeah, it makes sense do not show login page for those users. But there's a subtle difference: if you use "remember me" functionality ( see https://symfony.com/doc/cur... ) - then we do need to show login form for those users, i.e. users who authenticated as IS_AUTHENTICATED_REMEMBERED so that they can authenticate as IS_AUTHENTICATED_FULLY. IN some parts of our application we do want to require IS_AUTHENTICATED_FULLY from our users - it makes sense for pages that need extra security. For example, on KnpU we do require IS_AUTHENTICATED_FULLY to view invoices. That means if user has IS_AUTHENTICATED_REMEMBERED only and trying to view his invoice - he we'll be redirected to the /login page to log in again as IS_AUTHENTICATED_FULLY and after successful login he will be redirected back to the invoice page.
request.CRITICAL: Uncaught PHP Exception Symfony\Component\Security\Core\Exception\AuthenticationCredentialsNotFoundException: "The token storage contains no authentication token. One possible reason may be that there is no firewall configured for this URL." at /home/vagrant/code/tests/sf4/fosuser-starter-kit/vendor/symfony/security/Core/Authorization/AuthorizationChecker.php line 49 {"exception":"[object] (Symfony\\Component\\Security\\Core\\Exception\\AuthenticationCredentialsNotFoundException(code: 0): The token storage contains no authentication token. One possible reason may be that there is no firewall configured for this URL. at /home/vagrant/code/tests/sf4/fosuser-starter-kit/vendor/symfony/security/Core/Authorization/AuthorizationChecker.php:49)"} []
[2018-08-13 15:15:07] request.CRITICAL: Exception thrown when handling an exception (Symfony\Component\Security\Core\Exception\AuthenticationCredentialsNotFoundException: The token storage contains no authentication token. One possible reason may be that there is no firewall configured for this URL. at /home/vagrant/code/tests/sf4/fosuser-starter-kit/vendor/symfony/security/Core/Authorization/AuthorizationChecker.php line 49) {"exception":"[object] (Symfony\\Component\\Security\\Core\\Exception\\AuthenticationCredentialsNotFoundException(code: 0): The token storage contains no authentication token. One possible reason may be that there is no firewall configured for this URL. at /home/vagrant/code/tests/sf4/fosuser-starter-kit/vendor/symfony/security/Core/Authorization/AuthorizationChecker.php:49)"} []
[2018-08-13 15:15:07] php.CRITICAL: Uncaught Exception: The token storage contains no authentication token. One possible reason may be that there is no firewall configured for this URL. {"exception":"[object] (Symfony\\Component\\Security\\Core\\Exception\\AuthenticationCredentialsNotFoundException(code: 0): The token storage contains no authentication token. One possible reason may be that there is no firewall configured for this URL. at /home/vagrant/code/tests/sf4/fosuser-starter-kit/vendor/symfony/security/Core/Authorization/AuthorizationChecker.php:49, Symfony\\Component\\Security\\Core\\Exception\\AuthenticationCredentialsNotFoundException(code: 0): The token storage contains no authentication token. One possible reason may be that there is no firewall configured for this URL. at /home/vagrant/code/tests/sf4/fosuser-starter-kit/vendor/symfony/security/Core/Authorization/AuthorizationChecker.php:49)"} []
[2018-08-13 15:15:07] request.CRITICAL: Uncaught PHP Exception Symfony\Component\Security\Core\Exception\AuthenticationCredentialsNotFoundException: "The token storage contains no authentication token. One possible reason may be that there is no firewall configured for this URL." at /home/vagrant/code/tests/sf4/fosuser-starter-kit/vendor/symfony/security/Core/Authorization/AuthorizationChecker.php line 49 {"exception":"[object] (Symfony\\Component\\Security\\Core\\Exception\\AuthenticationCredentialsNotFoundException(code: 0): The token storage contains no authentication token. One possible reason may be that there is no firewall configured for this URL. at /home/vagrant/code/tests/sf4/fosuser-starter-kit/vendor/symfony/security/Core/Authorization/AuthorizationChecker.php:49, Symfony\\Component\\Security\\Core\\Exception\\AuthenticationCredentialsNotFoundException(code: 0): The token storage contains no authentication token. One possible reason may be that there is no firewall configured for this URL. at /home/vagrant/code/tests/sf4/fosuser-starter-kit/vendor/symfony/security/Core/Authorization/AuthorizationChecker.php:49)"} []
Yeah, if we're talking about FOSUserBundle - I think the easier way to do so is to add an even listener as in example you provided. What about the error, could you show the code that causes those errors?
How to do this redirecting to How to do this redirecting to the previous page with login controller.It doesn't have event listeners like registration controller?the previous page with login controller.It doesn't have event listeners like registration controller?
Great question! If you're using FOSUserBundle in the "normal" way, then you are using the "form_login" mechanism in security.yml. This login system *automatically* redirects back to the previous page, without you needing to do *any* work. This feature actually doesn't come from FOSUserBundle, but is just part of Symfony's core. So, it should automatically work how you want it to. But, here is more information about how to customize where it redirects: https://symfony.com/doc/cur...
Cheers!
Please, log in to vote for this comment
|
Share Comment
"Houston: no signs of life" Start the conversation!
12 Comments
Hi KNP,
I would like to create button in Twig to go back to whatever the previous page is and I found this code: "Go Back To Previous Page" Unfortunately tested it does not work in Symfony 4 default twig version - it will only return url of the current page. (source: https://stackoverflow.com/q....
How do we do this with Twig framework, is it possible?
Thank you!
Hey Dung L.
Hmm, I don't know why it's not working, getting the referer from the request seems like the right way to do what you want, but just be aware of it might be null or a hacky user could modify it (of course, it would only affect himself). Probably what you want is "breadcrumbs" https://github.com/whiteoct...
Cheers!
Excellent, another bundle (more service :) means for food for knowledge and more power, I will try it out and Thanks Diego!
Good Morning Dung L.
I have successfully implemented "breadcrumbs". And in regard to Twig template's output expression {{ app.request.headers.get('referer') }} the reason it did not work was because had attribute rel="noopener noreferrer" in my anchor tag - it works now after removing "rel"
Thank you for your pointers!
Hello KNP Team and thanks for your videos!
By the way, how can I handle ALREADY logged-in user if he goes to /login page?
In my case he sees login form still but has logged-in status in web-profiler bar.
Hey Lopoi,
Good question, the default behavior is to redirect already logged-in users to the homepage (or any other page that makes more sense). So yeah, it makes sense do not show login page for those users. But there's a subtle difference: if you use "remember me" functionality ( see https://symfony.com/doc/cur... ) - then we do need to show login form for those users, i.e. users who authenticated as IS_AUTHENTICATED_REMEMBERED so that they can authenticate as IS_AUTHENTICATED_FULLY. IN some parts of our application we do want to require IS_AUTHENTICATED_FULLY from our users - it makes sense for pages that need extra security. For example, on KnpU we do require IS_AUTHENTICATED_FULLY to view invoices. That means if user has IS_AUTHENTICATED_REMEMBERED only and trying to view his invoice - he we'll be redirected to the /login page to log in again as IS_AUTHENTICATED_FULLY and after successful login he will be redirected back to the invoice page.
Cheers!
Hey victor ,
looks like in SF4 the easiest way to redirect already logged-in user to route - to use guard system.
Or may be there is some event to intercept?
I tried this https://stackoverflow.com/questions/49585780/symfony-redirect-user-already-logged-in
solution but had error :
Please advise.
Hey Lopoi,
Yeah, if we're talking about FOSUserBundle - I think the easier way to do so is to add an even listener as in example you provided. What about the error, could you show the code that causes those errors?
Cheers!
victor , thanks for your quick reply!
I used exact code from SO link above and codebase from this KNP tutorial.
Error messages attached in this thread.
Actually I changed their code a little and replied also at stackoverflow https://stackoverflow.com/q...
Now all works as expected using kernel events. So it is not a problem anymore.
Thanks for your attention to my issue!
Hey Lopoi,
I'm glad you fixed the problem by yourself, it's even better than to get a ready-to-use solution from somebody. Great work!
Cheers!
How to do this redirecting to How to do this redirecting to the previous page with login controller.It doesn't have event listeners like registration controller?the previous page with login controller.It doesn't have event listeners like registration controller?
Hey Yasiru Nilan!
Great question! If you're using FOSUserBundle in the "normal" way, then you are using the "form_login" mechanism in security.yml. This login system *automatically* redirects back to the previous page, without you needing to do *any* work. This feature actually doesn't come from FOSUserBundle, but is just part of Symfony's core. So, it should automatically work how you want it to. But, here is more information about how to customize where it redirects: https://symfony.com/doc/cur...
Cheers!
"Houston: no signs of life"
Start the conversation!