简体   繁体   English

使用继承在Codeigniter项目中进行设计时遇到麻烦

[英]having trouble with design in codeigniter project, using inheritance

Iam currently reworking my project in codeigniter to set it up so that I have a User.php superclass. Iam当前在codeigniter中对我的项目进行了重新设置,以使我拥有一个User.php超类。 this class then has 2 subclasses "carer_user" and "admin_user" whenn logging i have a method called login in the super class whos index is called first. 此类在登录时有两个称为“ carer_user”和“ admin_user”的子类。 after this i will decide which user type has is being logged in and display the relevant page for that specific user type. 之后,我将确定正在登录的用户类型,并显示该特定用户类型的相关页面。 each is different. 每个都不同。 how i have it set up currently seems wrong to me as i have the login in the superclass User. 我现在如何设置它对我来说似乎是错误的,因为我已经在超类用户中登录了。 for each user type is set up slightly differently,runs different querys agains the db, creates session differently. 对于每种用户类型,设置略有不同,再次运行不同的查询,数据库,创建会话。 also i dont think its proper practice to call a subclass method from a super class one the login has been verified. 我也不认为从超级类中调用子类方法的正确做法是登录已被验证。 my question(s) is/are this. 我的问题是这个。 should login be in a controller class of its own? 登录应该属于自己的控制器类吗? once verification has been done here i can then call a method in either admin_user or carer_user depending on the result.or should i stick with the way iam doing it right now, my code is shown below for my User controller method. 一旦在这里完成验证,我就可以根据结果在admin_user或carer_user中调用一个方法。或者我现在应该坚持使用iam的方法,下面显示了我的User控制器方法的代码。 thanks! 谢谢!

<?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;


    }
}
}

?>

again my problem with this is where the login method should be placed? 再次,我的问题是应该在哪里放置登录方法? and the fact that i cant really have a superclass calling a method in one of its subclasses. 我不能真正拥有在其子类之一中调用方法的超类这一事实。

Multiple inheritance is not possible. 多重继承是不可能的。 You either can use interfaces. 您可以使用接口。 Please have a look here for better explanation https://github.com/sepehr/ci-base-controllers 请在这里看看以获得更好的解释https://github.com/sepehr/ci-base-controllers

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM