0

在大多数情况下,我认为我的代码现在很乱,可能是因为我真的不习惯 CI。我目前有一个名为“用户”的控制器,我将在下面显示。在这个控制器中,我有登录、注销等方法。这里有足够标准的东西,比如在用户登录时创建会话并在用户注销时销毁它。iam 还跟踪时间,因为系统模仿“签到”系统来让用户工作时间。项目的下一部分需要访问用户使用的数据库中的“客户”表,并且在按下按钮时将显示有关此客户的一些信息,客户本身不使用此系统,但有关他们的信息需要查看。我只是想知道我应该继续添加到我当前的控制器和模型还是接下来设置单独的类,在我看来控制器已经比较大了?这是我的控制器代码。

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class User extends CI_Controller {

    var $loggedin = FALSE;
    var $cdata;

    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()
{
        //get posted data. check if what is posted is in db.
        // if it is set loggedin=true + redirect to carer page + save data in session.        //get posted data. check if what is posted is in db.

        if(isset($_POST['email'])  )
            {$this->cdata['email'] = $_POST['email'] ;}
            else
            {$this->cdata['email'] ="";}
        if(isset($_POST['password']))
            {$this->cdata['password'] = $_POST['password'];}
            else
            {$this->cdata['password'] ="";}


        if($this->session->userdata('email'))
            {       
                $this->loggedin = true;
            }
        else
            {
                $this->loggedin = $this->dbaccess->check_input($this->cdata['email'],$this->cdata['password']);
            }




    if($this->loggedin === TRUE && !$this->session->userdata('email'))
        {

            $this->start_session();

            if(!$this->dbaccess->get_date_entry($this->session->userdata('email'),date("Y-m-d")))
            {
                $data =     array("email"=>$this->session->userdata('email'),"date"=>date("Y-m-d"),
                "hours"=>"0","starttime"=>date("Y-m-d     H:i:s",$this->session->userdata('last_activity')));
                $this->dbaccess->insert_daily_row($data);
            }

            $this->load->view('carerview',$this->cdata);
        }

    else if ($this->loggedin === TRUE && $this->session->userdata('email'))
        {
            $this->cdata['totalhours']= $this->session->userdata('totalhours');
            $this->cdata['logintime']= $this->session->userdata('last_activity');
            $this->load->view('carerview',$this->cdata);

        }

    else
        {
            $this->session->unset_userdata('last_activity');
            $this->cdata['warning']="Check failed ! Please try again";
            $this->load->view('mainview',$this->cdata);
        }
}


private function start_session()
{

    $this->load->library('session');
    $this->session->set_userdata('email',$this->cdata['email']);
    $this->cdata['totalhours']= $this->dbaccess->get_hours_by_date
    ($this->session->userdata('email'),date("Y-m-d",$this->session->userdata('last_activity')));
    $this->session->set_userdata('totalhours',$this->cdata['totalhours']);
}






private function calculatedifference($starttime,$endtime) // delete checkin time from checkouttime
{

    $diff=  abs(strtotime($starttime) - strtotime($endtime));
    $this->cdata['diff'] = date("H:i:s",$diff);
    $this->cdata['start'] = date("Y-m-d H:i:s", strtotime( $starttime)+(1 * 3600));
    $this->cdata['end']= date("Y-m-d H:i:s", strtotime($endtime) +(1 * 3600));
    return  date("H:i:s",$diff);

}


public function isLoggedIn()
{
    return $this->loggedin;
}



public function logOut()
{
    // update total hours and update checkout time.

    if($this->session->userdata('email')){
    $hours=$this->calculatedifference(date("Y-m-d H:i:s",$this->session->userdata('last_activity')),date("Y-m-d H:i:s"));
    $data =array("endtime"=>date("Y-m-d H:i:s"),"hours"=>$hours,"email"=>$this->session->userdata('email'),"date"=>date("Y-m-d"));
    $this->dbaccess->update_daily_row($data);}

        $this->load->view('mainview',$this->cdata);
        $this->session->sess_destroy();
}


public function admin()
{
    $this->load->view('adminpage',$this->cdata);
}







}
4

2 回答 2

1

好吧,使用 MVC 框架来结束单控制器应用程序听起来有点矫枉过正。也就是说,如果事实证明您的应用程序只需要实现一个控制器。

此外,您很可能需要不属于“用户”范围的功能。公共内容是一个很好(而且很明显)的例子。

此外,CI 在/controller/method/param0/param1/...本质上映射 url,因此仅保留User会使您的整个 url 命名空间挂在/user/路径下方,我认为这不是您想要的。

所以我会采用多控制器方式——或者更确切地说,如果我的设计暗示了这样的需求,我会使用 MVC 框架。

关于您的第二个问题,没有大类或小类(在您的情况下为控制器)之类的东西。这取决于 (1) 您如何为应用程序建模和 (2) 您的编码习惯。

于 2013-10-08T18:01:43.343 回答
0

注意上一个答案的建议,当您处理用户特定内容和公共内容时,这里还有一个小技巧。我有一个名为 user_base.php 的控制器,它在处理用户特定内容(如登录用户的数据)时总是首先加载:

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class User_base extends CI_Controller
{
//define for later use in the construct
public $user_id;
public $username;

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

    // here load everything you will need to service users.
    $this->load->helper('url');
    $this->load->library('my_functions');
    $this->load->model('my_model');
    $this->lang->load('app');

    if($this->my_functions->is_logged_in() ){
        // if user is really logged in, I prepare often needed variables            
        $this->user_id = $this->my_functions->get_user_id();
        $this->username = $this->my_functions->get_username();

        // here i define global variables that once loaded are usable by every view file
        $gv['logged'] = true;
        $gv['username'] = $this->username;
        $gv['user_id'] = $this->user_id;
        $gv['something'] = $this->my_model->some_function($this->user_id);

        $this->load->vars($gv); 
    }
    else{ // if user is not logged in, throw him out.
        redirect('');
    }


   }
  }
  ?>

现在,准备好这个基础后,我可以开始为用户创建控制器,例如用于用户设置、查看配置文件等的帐户。请注意,首先您总是首先加载基础文件,然后使用该基础扩展您的控制器。

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

 require 'user_base.php';

 class Accounts extends User_base{

private $var;

function __construct()
{
    parent::__construct();      
    $this->var = 'some value';
}

function index(){
       $data['value_from_DB'] = $this->my_model->get_stuff($this->user_id, $this->var);
       $this->load->view('viewAccount', $data);
}

另请注意,现在我可以轻松地从 user_base 加载所有函数以及本地定义的私有变量的用户 ID。

最后,在视图文件中,您现在可以调用 $username,因为它是在 user_base.php 中加载的:

<p>Hello there, <?php echo $username?></p>

然后我会对 public_base 和 admin_base 等做同样的事情。换句话说。我会为我能想象到的最广泛的类别定义基础,这些类别是互斥的,然后围绕它们创建较小类别的控制器,例如用于管理用户帐户(或您的情况下为“用户”)的所有功能的帐户,然后是所有视图的订单以及处理订单的报告和流程,或网站博客部分的博客等。

希望这有助于结构化和保持代码清洁。

于 2013-10-08T23:41:41.097 回答