how to override renderLogin fucntion of fosUserBundle? I am using symfony 4.4 and I want to pass some data to renderLogin function. Can you please help me with this? for example, which folder can I make a controller to extend from Securitycontroller? and do I need to set route? if yes, how? thank you
In the latest FOSUserBundle, controllers are services. So to override that method, you'll need to override the class of the service. It's not a super common thing to do:
A) Create a class that extends the SecurityController B) Create a compiler class that changes the fos_user.security.controller service to use your new class - I'd follow the 2nd example here, where you add the compiler pass to Kernel https://symfony.com/doc/current/service_container/compiler_passes.html
It will probably look something like:
public function process(ContainerBuilder $container): void
{
$container->getDefinition('fos_user.security.controller')->setClass('App\\Controller\\YourNewSecurityController');
}
Hey I'm using Symfony 4 , after adding the "remember_me" setting I got this error "You have requested a non-existent parameter "secret". Did you mean this: "kernel.secret"?"
That's because you haven't defined any "secret" parameter, and you don't have to acutally, in Symfony4 the secret is an environment variable usually named as "APP_SECRET", and you can use it like this:
Hello , first , I want thank you for this useful tutorial. just a remark to mention. I'm using Symfony 3.4,and in this version I think we should add the provider key in form_login ( security.yml). to let know symfony which provider to use when handling login request/action.
I am using Symfony 4. My routes for login are working but I don't know why the files haven't been imported to the App folder. Now, I have to edit the files in the vendor/FOS/user-bundle folder. Is this correct or am I missing something.
I don't fully get your situation. You won't get any files from the bundle except for a configuration file (and only if the bundle comes with a recipe, I think FOSUserBundle already has a recipe) inside "config/packages". Probably what you are missing is the config line that imports the routes from FOSUserBundle.
Hello Diego, thanks for the reply. I manually imported my twig files to templates/bundles/FOSUserBundle This is where I copied all my folders like ChangePassword, Profile, Security, etc. from the package. It works now and I can edit those files.
It would be better to use {% if is_granted("IS_AUTHENTICATED_REMEMBERED") %} instead of relying on if user has ROLE_USER to figure out if user is logged in.
You're right! Well, it depends if you use the "remember_me" functionality :). If you do NOT, then it doesn't make any difference - and so we only talked about ROLE_USER to simplify things. But if you *do* use the remember me functionality, then you're 100% correct - good note!
No route found for "GET /" (from "http://localhost/symfony/projects/test2/web/app_dev.php/login")
...when i'm trying logout is similar.
I have this in my security.yml : form_login: ~ logout: ~ logout: path: fos_user_security_logout form_login: login_path: fos_user_security_login check_path: fos_user_security_check csrf_token_generator: security.csrf.token_manager remember_me: secret: '%secret%'
Do you have a route for "/" URL in your Symfony app? Looks like you just need to declare a route for "/" or probably clear the cache if you have a declared one. You can debug it with:
That's happening because of the last "slash" in your path, it should be "/login" (without slash) but if you want to avoid those problems, you can follow this guide and make a redirect to the same route without a slash http://symfony.com/doc/curr...
Have a nice day!
Please, log in to vote for this comment
|
Share Comment
"Houston: no signs of life" Start the conversation!
18 Comments
how to override renderLogin fucntion of fosUserBundle?
I am using symfony 4.4 and I want to pass some data to renderLogin
function. Can you please help me with this? for example, which folder
can I make a controller to extend from Securitycontroller? and do I need
to set route? if yes, how? thank you
Hi @Nauman!
In the latest FOSUserBundle, controllers are services. So to override that method, you'll need to override the class of the service. It's not a super common thing to do:
A) Create a class that extends the SecurityController
B) Create a compiler class that changes the
fos_user.security.controllerservice to use your new class - I'd follow the 2nd example here, where you add the compiler pass to Kernel https://symfony.com/doc/current/service_container/compiler_passes.htmlIt will probably look something like:
Good luck!
Hey I'm using Symfony 4 , after adding the "remember_me" setting I got this error "You have requested a non-existent parameter "secret". Did you mean this: "kernel.secret"?"
Thank you !!
Hey @Amina
That's because you haven't defined any "secret" parameter, and you don't have to acutally, in Symfony4 the secret is an environment variable usually named as "APP_SECRET", and you can use it like this:
Cheers!
Thank you :)
Hello , first , I want thank you for this useful tutorial. just a remark to mention. I'm using Symfony 3.4,and in this version I think we should add the provider key in form_login ( security.yml). to let know symfony which provider to use when handling login request/action.
Hey Elmehdi GROLA
If you have a custom provider, then, yes, you have to change the config, but if not, I believe it will just fallback to FOSUserBundle user provider
Cheers!
I am using Symfony 4. My routes for login are working but I don't know why the files haven't been imported to the App folder. Now, I have to edit the files in the vendor/FOS/user-bundle folder. Is this correct or am I missing something.
Hey Ravee s G.
I don't fully get your situation. You won't get any files from the bundle except for a configuration file (and only if the bundle comes with a recipe, I think FOSUserBundle already has a recipe) inside "config/packages". Probably what you are missing is the config line that imports the routes from FOSUserBundle.
Cheers!
Hello Diego, thanks for the reply. I manually imported my twig files to templates/bundles/FOSUserBundle
This is where I copied all my folders like ChangePassword, Profile, Security, etc. from the package. It works now and I can edit those files.
It would be better to use {% if is_granted("IS_AUTHENTICATED_REMEMBERED") %}
instead of relying on if user has ROLE_USER to figure out if user is logged in.
Hey Trashcan4U!
You're right! Well, it depends if you use the "remember_me" functionality :). If you do NOT, then it doesn't make any difference - and so we only talked about ROLE_USER to simplify things. But if you *do* use the remember me functionality, then you're 100% correct - good note!
Cheers!
btw, funny name ::p
I have error when i try to log in:
No route found for "GET /" (from "http://localhost/symfony/projects/test2/web/app_dev.php/login")
...when i'm trying logout is similar.
I have this in my security.yml :
form_login: ~
logout: ~
logout:
path: fos_user_security_logout
form_login:
login_path: fos_user_security_login
check_path: fos_user_security_check
csrf_token_generator: security.csrf.token_manager
remember_me:
secret: '%secret%'
Any suggestion?
Hey Szymon,
Do you have a route for "/" URL in your Symfony app? Looks like you just need to declare a route for "/" or probably clear the cache if you have a declared one. You can debug it with:
bin/console debug:router
Cheers!
Hey Victor,
Thx for your answer.
You were right, i didnt have route "/". All my routes started with "/mojaApka/"
Now it works:).
All the best.
No route found for "GET /login/"
I get this error even tho I have :
fos_user:
resource: "@FOSUserBundle/Resources/config/routing/all.xml"
Hey Islam Elshobokshy
That's happening because of the last "slash" in your path, it should be "/login" (without slash)
but if you want to avoid those problems, you can follow this guide and make a redirect to the same route without a slash
http://symfony.com/doc/curr...
Have a nice day!
"Houston: no signs of life"
Start the conversation!