FreakAuth_light: custom userprofile - PART 2 is a follow up of the first part of this tutorial about FreakAuth_light custom userprofile functionality. Now we will build a frontend controller/views to display the profile of our users an to allow the owners of the profile to edit it.
If you missed part one please go to FreakAuth_light: custom userprofile - PART 1
Again I assume you have already installed both CI 1.5.2 and FreakAuth_light 1.0.2-Beta and that you are a bit familiar with them.
If you wanna play with custom userprofile in FreakAuth_light you should also have it enable in the FreakAith_light configuration:
The source code for this tutorial is available for download in the download section. Look for the file tutorial_custom_userprofile_part2.zip
I'll call the controller to handle the userprofile visualization/editing myaccount.Before strating we need to add a couple of lines in our application/config/routes.php.
Well let's start to build the controller to handle the visualization/editing of the userprofile of our users.
I'll call the controller Myaccount and I'll put it in a file called myaccount.php into the application/controllers/ folder:
Everybody will be able to see the userprofile of our users.
The method show() will take care of displaying the userprofile for the user id passed into the URI.
We are not ready yet, because we need a view for letting show() work properly.
Create a file called view.php with the following inside and drop it in your application/views/ folder:
Now go to www.yoursite.com/index.php/myaccount/show/1 and you will be able to see the profile of the user with id=1
Next step will be to add the method edit() in our controller in order to let our users to edit their own profile. Therefore we will need to restrict the access to this method not only to valid logged in users, but we will also check that they are the effective owners of the profile with the following statement:
The method edit() requires some custom validation sules (callbacks), I won't cover them here but you will find them in the ready to plug downloadable code.Then download the source code for this tutorial if you want to test the edit() method properly.
Let's open our file myaccont.php again and add the method edit() to the Myaccount class:
Ok, again for the method edit() to work we need to build a edit.php view and drop it in the application/views directory:
Now if you are:
go to www.yourdomain.com/index.php/myaccount/edit/1 and you will be able to edit your profile.
Well we are done with user profile visualization and editing. It would also be nice to add a method to our controller in order to display all registered users with a link to their public profile.
I live this last task to you: the method is ready in the Myaccount class and it is called index()=> just fill in the code in the curly brackets {//your code here...}
I hope this tutorial has helped you to discover a bit better the custom userprofile feature of FreakAuth_light_1.0.2-Beta.
Happy coding!
Dan