开发者

Code igniter extensible system?

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 co开发者_C百科uld 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?


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


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!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜