Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Creating a user registration form
  • Hi, I'm relatively new to FuelPHP; having worked with Wordpress for some considerable years I've got to the point where I need to break away from the restrictions it imposes on what I can do with it.

    I've been working through the 'FuelPHP Application Development Blueprints' book and am getting a good grasp of how to use FuelPHP and I'm really pleased with how flexible and powerful it is. However, there is one area where I'm really, really stuck on - its probably quite simple but no matter how much I've read the documentation and experimented I just can't fathom it out.

    I've followed the documentation and have been able to initially create a user with a command in the Oil console (I'm using the 'Ormauth' driver) with Auth::create_user. I also have the login form when I go to /admin (or /admin/logout).

    I understand that the registration example in the documentation is meant to be just that (an example) but I'm struggling to work out how to create a registration form which will 'talk' to the registration code, validate the fields and add the user.

    Users will only be added by a super administrator (no other user can register) so the form needs the users chosen username, full name. email address and group.

    Could someone point me in the direction (or provide an example) of how I would do this? I'm very much a visual learner so seeing a piece of code enables me to work out what's happening (as I've done with the book) would help me understand it better.

    Any advice would be most appreciated. :)
  • The Auth example code in the docs are a direct copy from the application framework we use as the basis of our applications. 

    Apart from the Message class, everything else is standard Fuel and should work without problems. You might have a look at https://github.com/fuel/depot/tree/1.0/develop/fuel/app/classes which has a version of this Message class.

    So maybe you can go into more detail of what you don't understand, what goes wrong, or what errors you have?
  • Hi Harro, thanks for the prompt reply. I'll try to clarify my issue.  

    I followed the steps as shown here 'http://bit.ly/2oIkfab'.

    I ran 'oil refine migrate --packages=auth' and 'oil generate admin pages title:string slug:string summary:text body:text publisher:int' as well as 'oil refine migrate'. 

    I've gone through the process of copying 'auth.php' and 'ormauth.php' to my config folder and set my salts. In 'auth' I've set the driver as 'Ormauth' and I've added 'auth' and 'orm' to my packages in 'always_load'. 

    I then ran 'php oil r migrate --all -v'. 

    Next I run in the oil console 'Auth::create_user('username','password''email',group number)' so I have my login added to the table. 

    If I go to '/admin/' I get the login form, can login and see the dashboard fine. I can also use '/admin/logout' to logout and see the form to login. 

    What I'm trying to work out is how to view the registration form so that future users can be added by the superadmin (group 6) without using the console. 

    In the documentation here 'http://bit.ly/2nl6Nsx' there's some example code for adding a new user registration. I've dropped that into controller > admin.php and got an error about the Message:: class which I replaced with a 'Session::get_flash' instead (I assume its okay doing that - no errors so far). 

    Now, I assume I go to '/admin/register' to display the registration form but all it does is point me back to the homepage (not even the admin area).

    If I then go to the admin area I see an error flash of 'login.registation-not-enabled'. I've tried to see where I enable it but can't figure out where I actually enable it!

    I'm assuming that the reason '/admin/register' takes me to the homepage is that I've not enabled registration but am not 100% sure (a) where I do that or (b) if that's the actual issue - is it something else?  

    I hope that clarifies everything, if I've missed anything out let me know and I'll elaborate as much as possible. 

    Thanks again for your help.
  • Stuff like

    if ( ! \Config::get('application.user.registration', false))

    also doesn't exist in your application, as the "application" config file is specific to our framework. Because it does not exist, the if is true, and you are redirected back.

    If you search in the examples on that page, you will see more references to that config file, we use it to control generic behavior of our apps depending on client requirements (i.e. some apps use active directory authentication, they don't need registration forms as they are for corporate use only).

  • Hmm, I see what you're saying - its referring to code which doesn't exist hence the redirect. 

    Can't say I'm any the wiser about setting a registration page up if I'm honest (sorry!), I'll have to spend some more time figuring things out (I suppose this is what happens when you've been spoiled with prebuilt admin areas in Wordpress. ModX and Drupal etc).

    I'm sure I'll fathom it out somehow, thanks for clarifying those points.
  • Harro VertonHarro Verton
    Accepted Answer
    In that if, just change the "false" default value to "true", so it skips the if.

    I think your biggest challenge here is that you don't have the View, and you struggle to understand the fieldset functionality, which is the biggest chunk of the code. Correct?

    What the code basically does:
    - it creates a fieldset object with the name "registerform"
    - it adds a hidden field for csrf checking
    - It adds all fields defined in the User model
    - It adds a fullname field after username (this is not in the user table, but in the meta data)
    - It adds a second password confirmation field
    - It disables the dropdown for group selection
    - It passes the fieldset to the view to be rendered

    In our app frameworks default skin, the fieldset renders to this form: http://imgur.com/a/pUfLH. You may have to read up on fieldsets, and perhaps play a bit with it, it is by far the most powerful, and most complicated part of Fuel.

    When the form is posted, it goes into the block: if (\Input::method() == 'POST')...

    There, if first validates the input. This is also part of the fieldset functionality. Since the fieldset knows all fields on the form, all field definitions (it gets that from the model) and all validation rules (either from the model, or added in the controller like the fullname and confirm fields), it is perfectly placed to do the validation. If validation failed (i.e. there were errors), it calls repopulate() to copy the values of POST back to the form fields, and displays the View again.

    If no errors where detected, it calls \Auth::create_user() to create the new user account in the Users table, and redirects away with a success message, or reloads the form with an error message.

    As to the Message class, see the link I posted earlier. You can copy the messages.php and messages/instance.php to your app/classes directory as is to use it.

    And finally, because it's you, here's the View that produces that screenshot: https://bin.fuelphp.com/snippet/view/OP.
  • Ah, I see, I see, I see. The lightbulb has flickered on (okay, its more like a candle) but I now understand the principles of how it works (the view is really useful - thanks for that).

    Will have another stab at it on Monday and see how I get on - thanks again for all your advice and taking the time to detail the process, its very much appreciated. :)

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion