4webby web applications
17
Jul 2007

Extending FreakAuth

In the new FreakAuth_light 1.1 we added a new nice feature that will make FAL customisation and maintainance much easier.


PREAMBLE (classes dependencies)


  • FAL_front depends on 2 classes: FreakAuth_light + FAL_Validation
  • FreakAuth_light -> independent
  • FAL_Validation -> extends Validation (depends on CI Validation)

 



We are basically talking about the implementation of an easy way to extend the ore classes.

In the FAL 1.1.0 release you will find 2 new files/classes in the application/libraries folder:

  • MyFal_demo.php
  • MyFALVal_demo.php


While the first is devoted to the extension of the core class/library, Freakauth_light (Freakauth_light.php), the latter is for the extension of the FAL_validation class (FAL_validation.php).

Extending the core classes requires a bit of knowledge of how classes work in PHP , and in particular on how their extension works. For those of you familiar with PHP classes extension nothing particulary new, while for those of you not confident with it we suggest you to check out the following pages of the PHP manual:

Said this, let's go on to discover how to extend FAL.

First of all rename the following files in application/libraries:

  • MyFal_demo.php to MyFal.php
  • MyFALVal_demo.php to MyFALVal.php

Then go in application/config/freakauth_light.php and turn extensions on:

application/config/freakauth_light.php
  1. $config['FAL_use_extensions'] = TRUE;

 

Basically if you need to add methods to the Freakauth_light class, add them into the MyFAL.php file.

If you need to add validation methods add them into the MyFALVal.php file.

In both cases be carefull to use NEW NAMES (namely different from those already in use in the Freakauth_light methods and FAL_validation methods): if you use the same names, your new functions will replace the core functions.

Said this you might already have understood that if you want to replace one of the original methods, you simply have to add a method with the same name of that you want to replace.

Let's make an example to make things clearer.

Let's say that for same reason you want to use a different function than that sed by FAL to encrypt your password. Then you should open MyFAL.php and add
the following method (_encode()) to the class MyFAL:

libraries/MyFAL.php
  1. if (!defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * Use it to add functions to the Freakauth_light.php class
  4. * or to overwrite its methods
  5. *
  6. */
  7. class MyFAL extends Freakauth_light
  8. {
  9. // --------------------------------------------------------------------
  10. /**
  11. * Class constructor
  12. * not really needed but I wrote it anyway
  13. *
  14. * @return MyFAL
  15. */
  16. function MyFAL()
  17. {
  18. parent::Freakauth_light();
  19. }

  20. // --------------------------------------------------------------------
  21. /**
  22. * this is an example: replaces the password encoding
  23. *
  24. * @param string $password
  25. */
  26. function _encode($password)
  27. {
  28. //your new encryption code here
  29. }
  30. }

 

The same applies to the other core functions.

Let's say you are not happy with the way FAL handles the registration process.
Well, open the file MyFAL.php and add a method register() to the MyFAL class.

The benefit of extending FAL this way is that you will be able to customise the core classes without effectively touch them.

This will be really handy when you will have to upgrade FAL with next eleases!

Happy coding ;o)

Dan