Having multiple registration form with fosuser bundle - symfony

I'm new to symfony2 & I'm writing an application that requires to have 2 different user types.
one is normal user & the other is the one which has same fields as user plus some other extra fields.
we design it with foreign key & one-to-one relationship.
Now I don't understand how to build two different registration forms with the functionality of FOSUserBundle.i mean i don't know how to override RegistrationController for this purpose
thanks for your answer :)

I would recommend using the PugxMultiUserBundle. I think that is exactly what you are after, I have been using it for one of my projects and it works like a charm!

You can use RollerworksMultiUserBundle:
https://github.com/rollerworks/RollerworksMultiUserBundle

Related

How to filter in Symfony2?

First of all i am very new to Symfony 2 and started to learn.
Is there any possibility for filter a value? Perhaps filter chains too?
I know this concept from Zend Framework 1 and 2.
E.g.:
i have a string "1A- N "
now i want to filter so that only numeric values pass; result "1"
Do i have to implement this on my own i Symfony?
I would like to do something like:
$text = '1A - N';
$numberFilter = new NumberFilter();
$filteredText = $numberFilter->filter($text);
//now in $text i find '1'
But for now i nowhere found something like this in Symfony what surprises me a lot. I thought it is a full stack framework and such function is so basic.
I found something like validators but they only say if a value e.g. contains only numbers or not. Or is the validation concept of symfony like that it does not only say if it is numeric or not but filter all other smybols out, too?
Depending on what you want precisely:
disallow user input not fulfilling certain rules
use validators in forms
use asserts in entities
chenge user input in case it's wrong
use viewransformers in forms
use event listeners in forms
use event listeners for doctrine
change data that already exists in the database
use filters in twig
create a command to execute from commandline
You can also try http://php.net/manual/ro/filter.filters.sanitize.php
I have built quite big apps with Symfony and never needed such a feature. Filters are mostly used in views anyway. Symfony comes with Twig, which has filters, that can be chained, and you can write your own filters. But if you need filters in the backend to do some background processing, you can accomplish it the way you suggested.
I suggest you write an interface and use a factory pattern, so you set a standard if you make many filters, so it will be easier to make the chaining work;)
After the answers and searching i got to the following conclusion.
There is no concept for this for now in Symphony 2.
You have to write it on your own.

Why we create Entity/Enquiry.php And Form/EnquiryType.php In Seperate Folders Symfony2?

Going through the Symblog tutorial of Symfony2, While creating forms I came to a point where in I create Contact Entity (Entity/Enquiry.php) where I define some fields and some methods to access these fields. Then I create another folder Form/EnquiryType.php to build the form and then a contact.html.twig to display. I am unable to understand why we created 2 namespaces for Entity/Enquiry.php and Form/EnquiryType.php. when they have to deal with each other. Why dont we wrote both the classes within one folder or one file. And one more question. Do they belong to Controller or View part of MVC.
Form types are here to configure how data coming from objects (like Entities) are mapped to a form (and vice/versa).
Entities should'nt be named "entities", they should be just your buisness objects, that can be persisted through a layer called doctrine2.
To answer you on separation of concerns,
Entities are about M,
while form Types are about user inputs (so the VC).
View because it render a human interface to let user enter input,
Controller because that's where you handle the form lifecycle.
The reason is logical separation. Why don't we define all parts of MVC in one folder/namespace? Because it will be a mess. That's why logical separation is needed.
And not all entities have to have related form types — using entities without forms is normal.

best and right way toorganize bundles in symfony2

I'm a little bit confused how I should organize the bundles in symfony2. In my app I'll need 3 pages:
1- Insert
2- List
3- Update
Which one could be the right and best way to organize my code?
src/cp/AddPageBundle
src/cp/EditPageBundle
src/cp/UpdatePageBundle
OR
In one bundle write 3 different controllers, each one in a different file?
OR
In one bundle, write 3 different actions in one controller file?
I'm really confused with this.
Thanks in advance!
In one bundle, 3 different actions in one controller. There is no need to split this functionality across bundles or controllers.
For example you can edit/list/update User with UserController (insertAction, ListAction, UpdateAction) to deal with the user and for example add another controller (CommentsController) for edit/list/update comments. The same situation can be used for your Page example (add/edit/update)
Optional way is to create folder inside controller folder so that we have even more organized code. For example for create Admin folder for controllers: Admin/ConsoleController, Admin/CategoryController, Admin/PluginController to deal with admin functionality.
Exactly I agree with TroodoN-Mike. Also you should create PageBundle with your Page entity and your fields (publish_date, title, content ect), and execute
app/console generate doctrine:crud PageBundle:Page
Symfony will generate a basic CRUD, but with your insert/list/update
just tried this - command is:
app/console doctrine:generate:crud --entity PageBundle:Page
This starts up a wizard on the command line which will prompt you for any other parameters it needs.
Unfortunately only works if your primary key field is called id and has a getID() method.

Using Roles in MVC 3 VB.NET App

I have an issue to do with roles in mvc 3 vb.net app..Say I have Admin, Developer, PowerAdmin roles.. if I want to restrict view options based on roles I have been using a if statement in the view to hide the link all together such as:
#If HttpContext.Current.User.IsInRole("Admin") And Request.IsAuthenticated Then
#<li>Administrative Tools</li>
End If
I am also decorating controller actions with authorize in places. The Problem is this say I have several actions that should only be available to say a user who is in all three roles or even 2 of the roles in any combination.. Would I simply nest the if statements in the view to hide those view items? What about controller functions.. Is it possible to decorate controller functions with something like
<Authorize(Roles:="Admin" + "PowerAdmin")>
and then have that function only accessible by someone with both roles????
simple write two separate lines :
<Authorize(Roles := "Admin")> _
<Authorize(Roles := "PowerAdmin")> _
Try googling on Enum; it's the perfect class for these kind of things.
Hope this will get you going on the right track.

Symfony2 User Roles (BETA2)

What's the appropriate way to add a user to a role.. for every new user, do I have to do:
$em->getRepository('MyBundle:Role')->findOneBy(array('name' => 'ROLE_USER'))
every time?
I'm not too much of a fan of how large UserBundle is.... and I don't use XML. I'm using YML/Annotations, so the UserBundle is pretty hard to follow for certain things.
So yes, what's the best / cleanest way to do a user signup and associate him to a default role?
The simplest way I've found is just to define roles as a field of type array on your User object. Then, when you're creating the user (on registration or whatnot), it's as simple as
$roles = array('ROLE_USER');
$user->setRoles($roles);
I've put together a mockup of my user registration process in this gist. It's not fully functional (I can flesh it out later if needed), but it should get you pointed in the right direction.
I wrote a couple of blog posts on roles, a simple solution http://blog.jmoz.co.uk/symfony2-fosuserbundle-roles and http://blog.jmoz.co.uk/symfony2-fosuserbundle-role-entities

Resources