Iam目前正在codeigniter中重新设计我的项目以进行设置,以便我拥有一个User.php超类。然后这个类有2个子类“carer_user”和“admin_user”当我在第一次调用索引的超类时有一个名为login的方法。在此之后,我将决定登录哪个用户类型并显示该特定用户类型的相关页面。每个都不同。我如何设置目前似乎是错误的,因为我在超类用户登录。对于每个用户类型设置略有不同,再次运行不同的查询db,创建不同的会话。我也不认为从超级类中调用子类方法的正确做法已经验证了登录。我的问题是这个。应该登录自己的控制器类?一旦验证完成,我就可以在admin_user或carer_user中调用一个方法,具体取决于结果。或者我应该坚持使用我现在这样做的方式,我的代码如下所示为我的用户控制器方法。谢谢!
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class User extends CI_Controller {
var $loggedin = FALSE;
var $cdata;
var $privilege;
function __construct()
{
parent::__construct();
$this->load->model("dbaccess");
$this->cdata =array( "warning" => "","email"=> "","password"=> "","logintime"=>"","start"=>"","end"=>""
,"diff"=>"","totalhours"=>"","dis"=>$this);
}
public function index()
{
if($this->session->userdata('email'))
{
$this->load->view('carerview',$this->cdata);
}
else
{
$this->load->view('mainview',$this->cdata);
}
}
public function login()
{
if(isset($_POST['email']) && isset($_POST['password']))
{
$this->cdata['email'] = $_POST['email'] ;
$this->cdata['password'] = $_POST['password'] ;
}
if($this->cdata['email'] !=="" && $this->cdata['password'] !=="" && $this->loggedin === FALSE)
{
$this->loggedin = $this->dbaccess->check_input($this->cdata['email'],$this->cdata['password']);
if($this->loggedin)
{
$data =array("email"=>$this->cdata['email']);
$this->privilege = $this->dbaccess->get_privilege($data,"userinfo");
$this->open_page();
$this->loggedin= TRUE;
}
else
{
$this->cdata['warning']="Check failed ! Please try again";
$this->load->view('mainview',$this->cdata);
}
}
else if($this->loggedin ===TRUE)
{
//check helpermethod. go to relevant page.
$this->open_page();
}
else
{
$this->cdata['warning']="Check failed ! Please try again";
$this->load->view('mainview',$this->cdata);
}
}
private function open_page()
{
switch($this->privilege)
{
case 0 :
$this->carerview();
break;
case 1:
$this->admin();
break;
}
}
}
?>
我的问题再次出现在哪里应该放置登录方法?事实上我真的没有超类在其子类之一中调用方法。