在另一个控制器中对codeigniter使用离子认证认证
我正在尝试使用codeigniter构建Web应用程序。我已将Ion Auth安装为我的身份验证模型。在另一个控制器中对codeigniter使用离子认证认证
默认Auth.php控制器对用户进行认证,并建立会话。
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class Auth extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->library('ion_auth');
$this->load->library('session');
$this->load->library('form_validation');
$this->load->helper('url');
$data['title']="Login Page";
$this->load->view("view_site_header",$data);
// Load MongoDB library instead of native db driver if required
$this->config->item('use_mongodb', 'ion_auth') ?
$this->load->library('mongo_db') :
$this->load->database();
$this->form_validation->set_error_delimiters($this->config->item('error_start_delimiter', 'ion_auth'), $this->config->item('error_end_delimiter', 'ion_auth'));
}
//redirect if needed, otherwise display the user list
function index()
{
// if not logged in - go to home page
if (!$this->ion_auth->logged_in())
{
//redirect them to the login page
redirect('auth/login', 'refresh');
}
// if user is an admin go to this page
elseif ($this->ion_auth->is_admin())
{
// if an admin, go to admin area
//set the flash data error message if there is one
$this->data['message'] = (validation_errors()) ? validation_errors() : $this->session->flashdata('message');
//list the users
$this->data['users'] = $this->ion_auth->users()->result();
foreach ($this->data['users'] as $k => $user)
{
$this->data['users'][$k]->groups = $this->ion_auth->get_users_groups($user->id)->result();
}
$this->_render_page('auth/view_users', $this->data);
} else
{
//redirect them to the default home page
$data['title']="IMS Home Page";
$this->load->view("generic/view_site_header",$data);
$this->load->view("generic/view_generic_nav");
$this->load->view("generic/view_content_generic");
$this->load->view("view_site_footer");
}
}
我想要做的是为我的应用程序逻辑创建一个新的控制器,并让身份验证控制器进行身份验证。
我如何可以使用身份验证控制,以确保我的用户的访问我的新控制器时,在登录?此外,我需要将激情信息提供给新控制器。
我的新的控制器,master_data具有下面的代码:
<?php if (! defined('BASEPATH')) exit('No direct script access allowed');
class Masterdata extends CI_Controller{
function index()
{
$data['title']="Master Data Home Page";
$this->load->view("master_data/view_master_data_header",$data);
$this->load->view("master_data/view_master_data_nav");
$this->load->view("master_data/view_content_master_data_home");
$this->load->view("master_data/view_master_data_footer");
echo $this->session->userdata('username');
}
}
显然echo $this->session->userdata('username');
不工作作为新的控制器没有在auth控制器会话的知识。
任何帮助表示赞赏一如既往。
此致
首先自动载入ion_auth库。 如果妳只是要检查,如果用户登录,就检查它在每一个控制器的构造函数ü加载
public function __construct() {
parent::__construct();
if (!$this->ion_auth->logged_in()) {
// redirect to login view
}
}
如果妳碰巧有多个组,U可以建立内部应用程序/内核的新型控制器/ MY_controller.This控制器将检查用户是否已登录。您可以扩展此基本控制器以创建新控制器。有关此问题的非常好的解释由David john提供。请检查此link。
明显回波$这 - >会话级>用户数据( '用户名');不起作用,因为新控制器不知道授权控制器会话。
呃......如果会话库被加载,那么是......控制器调用它将能够访问会话变量$ username。
我们处理这个问题的方法是在应用程序/核心目录下创建像MY_Controller一个新的控制器父类。这个类加载公共库/包(如session和ion_auth)。您也可以自动加载库和帮助程序。
由于ion_auth存储所有会话中的变种的用户配置文件数据的,所有你需要(在后续的,非认证)的网页是会话LIB检索有关登录用户的会话数据。
你真的应该检查他们的身份验证状态,虽然,优雅地失败:
if (!$this->ion_auth->logged_in()) {
// echo a login link
} else {
// echo session var for username
}
类似的东西...
谢谢jcorry,应用程序/核心目录中的MY_Controller,是否自动处理这段代码而不自动加载?所以如果我将我的离子认证控制器语法移到MY_Controller,这会自动在所有将来的控制器中使用吗?这是一种标准做法吗?感谢您的知识和时间。 – Smudger 2013-02-28 20:00:02
你对MY_Controller的处理是用你自己的方式重载CI控制器类。因此,'class MY_Controller extends CI_Controller'可以让你继承CI控制器类的所有功能,但是可以对从MY_Controller继承的单个控制器进行更改。你可以在这里阅读这个原理:[链接](http://ellislab.com/codeigniter/user-guide/general/core_classes.html) – jcorry 2013-03-04 15:09:30
jcorrys办法应该工作。另一种方法(这将使您的整个应用程序具有更大的灵活性)使用模块化布局 - https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc
您必须做一些小小的调整才能使其与ion认证一起正常工作,但遵循说明在这个问题上为我工作:Using Ion Auth as a separate module in the HMVC structure(看看git hub上的离子认证的分支 - 我认为有人可能已经为你做了)
这种方法将允许你访问任何控制器中的任何方法在应用程序中的任何地方使用这种语法(即使从视图如果需要):modules::run('module/controller/method', $params);
这将ESSENTIA允许您将现有的离子认证控制器开发成用户管理控制器,您可以从您创建的任何其他控制器(好和干)访问该用户管理控制器。
对于简单的安全控制器来说,这是一个比通过MY_Controller进行扩展更简单的方法。 – jaredstenquist 2013-03-21 03:08:25