4webby web applications
23
May 2007

I don't like that "auth.php" controller!

This tutorial will introduce you to the customization of the auth.php controller in FreakAuth_light.

I'll refer to FreakAuth_light library as FAL.

Requirements: FreakAuth_light_1.0.4

If you have FAL_1.0.4 let's go!

Well since the release of FA_1.0.4 you can build your own auth.php controller, or even split its methods (login, register etc.) into multiple controllers, and call them as you like. We introduced some new config variables that will help you in achieving this. The new config variables in application/config/freakauth_light.php are:

application/config/freakauth_light.php
  1. /*-------------------------------------+
  2. | CUSTOM CONTROLLERS NAMING SETTINGS |
  3. +-------------------------------------*/

  4. /*
  5. | You can name the controllers and relative actions as you like.
  6. | Namely you can place the login, logout, register, changepassword... actions
  7. | in a single controller (like we did in the demo application),
  8. | or in several controllers at your choice.
  9. | If you want to use this freedom you must set/change the following parameters.
  10. | For each of the following settings you must provide the controller_name/method_name
  11. | (or simply the controller_name if you just have a index() method in it.
  12. | --------
  13. | NOTE!
  14. | --------
  15. | For
  16. | - $config['FAL_activation_uri']
  17. | - $config['FAL_forgottenPasswordReset_uri']
  18. | it is compulsory to specify both the controller/method even if the
  19. | method is index()
  20. */
  21. $config['FAL_login_uri'] = 'auth/login';
  22. $config['FAL_logout_uri'] = 'auth/logout';
  23. $config['FAL_register_uri'] = 'auth/register';
  24. $config['FAL_activation_uri'] = 'auth/activation'; //read the above note
  25. $config['FAL_changePassword_uri'] = 'auth/changepassword';
  26. $config['FAL_forgottenPassword_uri'] = 'auth/forgotten_password';
  27. $config['FAL_forgottenPasswordReset_uri'] = 'auth/forgotten_password_reset'; //read the above note


I'll be a bit less cryptic in explaining what we are gonna get with this tutorial: with the FAL demo you download you have a controller called auth.php, that handles everything. Therefore you will have uri's like these:

  • www.yourwebsite.com/index.php/auth/login
  • www.yourwebsite.com/index.php/auth/register
  • etc.

In this tutorial I'll explain you how to get to uri's like these instead:

  • www.yourwebsite.com/index.php/login
  • www.yourwebsite.com/index.php/register
  • etc.

If you don't like the auth.php controller, and in particular its name, or the fact that it contains all those methods you have 2 ways in order to proceed:

  1. re-routing: this should work even with earlier versions of FAL
  2. re-factoring: build multiple contollers

Let's have a look at method number 1.

Re-routing the auth controller

If you read the userguide and in particular URI Routing you can guess what I'm gonna tell you here.

Let's start modifying the configs:

application/config/freakauth_light.php
  1. $config['FAL_login_uri'] = 'login';
  2. $config['FAL_logout_uri'] = 'logout';
  3. $config['FAL_register_uri'] = 'register';
  4. $config['FAL_activation_uri'] = 'activate/index';
  5. $config['FAL_changePassword_uri'] = 'changepass';
  6. $config['FAL_forgottenPassword_uri'] = 'forgpass';
  7. $config['FAL_forgottenPasswordReset_uri'] = 'resetpass/index';

 

Open your application/config/routes.php and add the following code:

application/config/routes.php
  1. $route['login'] = "auth";
  2. $route['login'] = "auth/login";
  3. $route['logout'] = "auth/logout";
  4. $route['register'] = "auth/register";
  5. $route['activate/:any'] = "auth/activation";
  6. $route['changepass'] = "auth/changepassword";
  7. $route['forgpass'] = "auth/forgotten_password";
  8. $route['resetpass/:any'] = "auth/forgotten_password_reset";

Routes must be set according to the config specified above!

You might have noticed that I used the :any uri routing wildcard for the activate and resetpass routes: I did so because these two methods require some parameters to be passed to the their uri in order to work properly.

Try to point your browser to:

  • www.yourwebsite.com/index.php/login

and you should be able to login! The same should work for the other re-routed controllers/methods.

Re-factoring the auth controller

With the re-factoring method (method number 2) we will build as many controllers as we like.

The methods activation and forgotten_password_reset get some parameters. If you build a specific controller for those 2 methods, as reported here, with FAL_1.0.4 you should place the controllers in the main application/controllers/ folder: DON'T PLACE THEM IN SUBFOLDERS

Here I show you how to build a controller for logging in and one for the logout.

Let's start modifying the configs:

application/config/freakauth_light.php
  1. $config['FAL_login_uri'] = 'login';
  2. $config['FAL_logout_uri'] = 'logout';
  3. $config['FAL_register_uri'] = 'register';
  4. $config['FAL_activation_uri'] = 'activate/index';
  5. $config['FAL_changePassword_uri'] = 'changepass';
  6. $config['FAL_forgottenPassword_uri'] = 'forgpass';
  7. $config['FAL_forgottenPasswordReset_uri'] = 'passreset/index';

In order to use the config as in the above box you must code a controller for each action specified.

With regard to your configuration in CUSTOM CONTROLLERS NAMING SETTINGS, while in generale you can omit to specify the method id you use the index() one to place your code in, for
- $config['FAL_activation_uri']
- $config['FAL_forgottenPasswordReset_uri']
it is compulsory to specify both the controller/method even if the method is index().
Why? Because these two methods get some parameters from the uri!

Let's build the Login controller and place it in application/controllers/

application/controllers/login.php


  1. class Login extends Controller
  2. {
  3. /**
  4. * Class constructor
  5. */
  6. function Login()
  7. {
  8. parent::Controller();

  9. $this->load->library('FAL_front', 'fal_front');

  10. $this->_container = $this->config->item('FAL_template_dir').'template/container';
  11. }

  12. // --------------------------------------------------------------------

  13. /**
  14. * Displays the login form.
  15. * uses the FAL templating way with the container
  16. */
  17. function index()
  18. {
  19. $data['fal'] = $this->fal_front->login();
  20. $this->load->view($this->_container, $data);
  21. }
  22. }

Try pointing your browser to

  • www.yourwebsite.com/index.php/login

The logout controller will look pretty similar to the login one. Let's build the Logout controller and place it in application/controllers/

application/controllers/logout.php


  1. class Logout extends Controller
  2. {
  3. /**
  4. * Class constructor
  5. */
  6. function Logout()
  7. {
  8. parent::Controller();

  9. $this->load->library('FAL_front', 'fal_front');

  10. $this->_container = $this->config->item('FAL_template_dir').'template/container';
  11. }

  12. // --------------------------------------------------------------------

  13. /**
  14. * Displays the logout form.
  15. * uses the FAL templating way with the container
  16. */
  17. function index()
  18. {
  19. $data['fal'] = $this->fal_front->logout();
  20. $this->load->view($this->_container, $data);
  21. }
  22. }

Try pointing your browser to

  • www.yourwebsite.com/index.php/logout

Not so complicated! Is it?

You should go on and build the controllers for the other methods that you set in your application/config/freakauth_light.php in the section "CUSTOM CONTROLLERS NAMING SETTINGS".

 

I hope this tutorial helped you out in discovering FreakAuth_light a bit more!

Happy coding ;O)

Dan