【问题标题】:Integrating CodeIgniter libraries (like tank auth)集成 CodeIgniter 库(如 tank auth)
【发布时间】:2013-05-09 00:11:14
【问题描述】:

我做了很多研究,但没有找到令人满意的答案。

我应该如何使用 CodeIgniter 库,例如 tank auth?我找到了一些方法,但它们似乎都乏善可陈:

  1. 我是否主要按原样使用控制器,根据需要添加控制器功能/包括样式?
  2. 我是否使用控制器作为示例来模拟我自己的模型,依赖对 $this->tank_auth 的调用和 tank auth 中包含的视图?
  3. 或者我是否使用 tank-auth 控制器扩展 MY_Controller,然后为任何需要身份验证的特定控制器扩展它,然后调用 parent::login()(或 register()、activate() 等)?李>

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

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

最后一个看起来很老套,对我来说似乎是反 MVC,但也许我错了。

【问题讨论】:

    标签: php codeigniter tankauth


    【解决方案1】:

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

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

    应用程序/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 附带的视图、自定义它们或创建自己的视图 - 由您决定。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-26
      • 1970-01-01
      • 2011-04-24
      相关资源
      最近更新 更多