Pregunta

I am writing a authentication Library for Code Igniter (for practice, so please do not suggest e.g. Tank Auth or DX Auth).

The library has basic login functionality, it also has the ability for Facebook and Twitter Logins.

However the Facebook and Twitter login code is hard coded into the Library. Which means that if I decide to add in e.g. Google Logins I have to modify the core of the Library. I don't like this.

I am wondering what ways I could make the library extensible so that Facebook and Twitter logins are "Modules" that can be added.

In this way someone could write a module for Google logins as well and the core of the system would not have to be modified.

How can I make extensible libraries in code igniter?

¿Fue útil?

Solución

This sounds like drivers in codeigniter is the right fit for this task. Take a look at the documentation : http://codeigniter.com/user_guide/general/creating_drivers.html

Otros consejos

Basically you'll need abstract classes (checkout http://nl2.php.net/manual/en/language.oop5.abstract.php)

In this particular case you would write something like this;

class Auth {
    abstract function login() {}
    abstract function logout() {}
}
class Twitter_Auth extends Auth {
    function login() { // the login magic }
    function logout() { // the logout magic }
}

if($login_type == 'twitter') {
    $auth = new Twitter_Auth();
} else {
    $auth = new Basic_Auth();
}
$auth->login();

--- edit you might be interested in how kohana handles this type of extension, eg. the auth module has several drivers. Checkout http://kohanaframework.org/3.2/guide/api/Auth, you can learn a lot from this!

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top