2013-05-09 91 views
0

我已经做了相当多的研究,并没有找到一个令人满意的答案。集成CodeIgniter库(如坦克认证)

我应该如何使用CodeIgniter库,如坦克认证?我已经找到办法了一把,但他们似乎都有点黯淡:

  1. 我用控制器大多原样,根据需要添加控制器功能/包括样式?
  2. 我是否使用控制器作为示例来建立自己的模型,依靠调用$ this-> tank_auth和tank auth包含的视图?
  3. 或者我用tank-auth控制器扩展MY_Controller,然后扩展那些需要认证的特定控制器,并调用parent :: login()(或register(),activate()等)?

第一个选项似乎是最好的,但现在看来似乎是难以避免复制大量的代码(会发生什么,如果我想要一个登录表单,但不想重定向到/ auth /中登录?)

第二个选项有同样的问题,但更糟。每次我想使用login_form视图时,我都需要包含tank auth控制器的登录函数的逻辑,对吗?

最后看起来真的很诡异,似乎反MVC给我,但也许我错了。

回答

1

退房http://philsturgeon.co.uk/blog/2010/02/CodeIgniter-Base-Classes-Keeping-it-DRY

伪代码(见链接 - 真的很好的例子有):

的application/config/config.php文件

$autoload = array('tank_auth'); 

// add this to the bottom of config.php as per the directions in the link 
function __autoload($class) 
{ 
    if(strpos($class, 'CI_') !== 0) 
    { 
     @include_once(APPPATH . 'core/'. $class . EXT); 
    } 
} 

应用/核心/ Private_Controller.php

class Private_Controller extends CI_Controller 
{ 
    function __construct() 
    { 
     parent::__construct(); 

     if(! $this->tank_auth->is_logged_in()){ 
      redirect('auth/login'); 
     } 
    } 
} 

控制器

class PrivateStuff extends Private_Controller { 
    function index() { 
     echo "you are logged in"; 
    } 
} 

-

http://example.com/index.php/privatestuff/ 
>you are logged in 

至于意见,你可以使用与LIB出来的那些,因为它们,自定义它们,或者创建自己的 - 给你。