CodeIgniter:使用核心类和扩展类?

时间:2014-02-21 12:46:57

标签: php codeigniter authentication extends

我目前正在为我的网站制作CI,而且我在扩展Controller_CI方面遇到了一些麻烦。 我有一个控制器处理登录/登录操作,不需要身份验证,其他控制器在加载内容之前检查用户会话是否存在。

为此,我创建了MY_Controller类并在构造函数中添加了身份验证代码。 然后我让我的所有控制器扩展MY_Controller,除了第一个仍然扩展Controller_CI

我的问题是:这是处理身份验证的正确方法吗?即使它被扩展,它仍然可以使用Controller_CI吗?

我发现了另一种模式:

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

我想它会更好,但我仍然不明白为什么不使用第一个解决方案。

由于

4 个答案:

答案 0 :(得分:0)

为此目的扩展控制器类将起作用,但此解决方案不够灵活。我宁愿创建一个处理身份验证的库,并在需要时从控制器运行它。有关在CI中创建自定义库的详细信息,请阅读http://ellislab.com/codeigniter/user-guide/general/creating_libraries.html

答案 1 :(得分:0)

请记住,您只能使用MY_Controller扩展CI_Controller一次。在这方面,这不是一个好主意。假设您想要为某些控制器实现另一个功能(例如,在日志中生成特定条目的一段代码),但不一定是需要身份验证的控制器,您无法创建另一个MY_Controller。

使用库是一件更好的事情。

我在大型CI网站上使用flexi auth库。在每个需要身份验证的控制器上,我只需添加以下内容:

public function __construct() {
    parent::__construct();

    $this->load->library('flexi_auth');

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

答案 2 :(得分:0)

我认为Phil Sturgeon在博文中建议和使用图书馆的组合最好。所以我会创建一个核心控制器(我的意思是你放入application/core扩展CI_Controller的控制器),名为MY_Controller,看起来像这样

class MY_Controller extends CI_Controller
{
    function __construct()
    {
        parent::__construct();
    }

    //Any other functions you want
}

然后根据您的问题判断您目前拥有适合两个类别的控制器

  • 执行的控制器需要先登录用户才能执行此操作 任何
  • 的控制器在执行任何操作之前需要登录用户

所以我会在/ application / core目录中创建另一个扩展MY_Controller的控制器,但是在它的构造函数中它会检查用户是否已登录

class Auth_Controller extends My_Controller
{
    function __construct()
    {
        parent::__construct();

        //Check to see if the user is logged in
        $this->load->library('authentication');

        if(!$this->authentication->user_logged_in())
        {
            redirect('/login');
        }
    }

    //Any other functions you want
}

现在,当您创建控制器时,您可以选择要扩展的核心控制器中的哪一个。如果它的控制器不是需要登录用户,则可以扩展MY_Controller,但如果 需要登录用户,则可以扩展Auth_Controller。这意味着您只需要在代码中执行一次用户登录检查。

与其他人一样,如果将任何身份验证代码放入库中可能是一个好主意,因为这是一个比在控制器中放置它更好的地方。

<强>摘要

总而言之,通过让所有控制器扩展核心控制器而不是CI_Controller,它应该减少代码重复。

答案 3 :(得分:0)

我目前正在研究CI项目并遇到同样的问题。我想出了一个不同的解决方案来处理身份验证。

我将核心控制器扩展为吼叫,

类MY_Controller扩展了CI_Controller {     public $ data = array();     public $ calledClass;     public $ calledMethod;

public function __construct()
{
    parent::__construct();

    $authException['authentication']['login'] = true;
    $authException['authentication']['logout'] = true;
    $authException['welcome']['index'] = true;

    $this->load->library("router");

    $this->calledClass = $this->router->fetch_class();
    $this->calledMethod = $this->router->fetch_method();

    if(!@$authentication[$this->calledClass][$authentication->calledMethod] && !Auth::isUserLoggedIn())
    {
        # IS_AJAX is a contant defined in the contants.php
        # define('IS_AJAX', isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest');
        if(IS_AJAX)
        {
            # if this is an AJAX call, it sets a value in the header ,
            # which can be captured in the AJAX response
            # to redirect the user to the login page.
            $this->output->set_header("auth-him:1");
            exit;
        }
        else
        {
            redirect("authentication/login");
        }
    }
}

}

希望上面的代码清楚,这有帮助。

要多次扩展核心控制器,如果仍需要使用2个控制器来处理身份验证,我已经按照这种方法进行了

https://stackoverflow.com/a/22125436/567854

希望这也有助于:)