I'm quite new in symfony2 and I'm using FOSUserBundle in my project for managing the users. I have a situation, that I have to create different type of users, which will need different controller actions/view templates.. My idea was to extend the register functionality of the FOSUserBundle and pass a parameter which will decide which kind of user will be created. For example if I go to register/candidate it will result a new candidate type user, but if I go to register/company I will get a company user. With this idea I could implement to show 2 different forms(formtype), but I got stucked at the controller side, how to pass the type parameter to the action controller, and confirm controller. I'm not sure if this idea could work so any help will be appreciated or if there is a more professional solution please share with me. If my example is not clear I can provide code snippets.

Thank you in advance.

有帮助吗?

解决方案 2

I found a quite simple solution for my question:

Adding a parameter to the route of registration:

<route id="fos_user_registration_register" pattern="/{utype}">
    <default key="_controller">FOSUserBundle:Registration:register</default>
    <default key="utype"></default>
</route>

So in the controller I could use this parameter in a condition to decide which type of registration I need:

//.....
$regtype = $request->get('utype');
if ($regtype ==='candidate')
    $type = 'candidateform';
if ($regtype ==='company')
    $type = 'companyform';

if (!empty($type))
    $formFactory->setType($type);
//.....

if ($form->isValid()) {
//....
if ($regtype === 'candidate'){
//...
}
if ($regtype === 'company'){
//..
}

In this way I can control the form rendering, also the action.

其他提示

Handle different type of user , you should use this bundle: https://github.com/PUGX/PUGXMultiUserBundle

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top