codeigniter 3:我的代码是否遵循MVC规则

时间:2016-10-05 09:28:16

标签: php codeigniter

我是codeigniter 3的新手,并试图通过此框架将我的PHP项目转换为MVC

控制器文件

class home extends CI_Controller {
    function __construct()
    {
        parent::__construct();
        $this->load->model('frontend/M_Headers');
    }   
    public function index()
    {
        $data['slide_image'] = $this->M_Headers->get_all_slide();
        $this->load->view('frontend/headers',$data);

    }
}

模型文件

class M_Headers extends CI_Model {
    public function __construct()
    {
           parent::__construct();
    }
    public function get_all_slide()
    {
        $query = $this->db->get('tbl_slide');
        return $query->result();

    }
}

查看档案

        <div class="slider">
            <ul class="rslides" id="slider">
<?php       
        if(count($slide_image) > 0)
        {
            foreach($slide_image as $value):
                $get_image=$value->sl_image;
                if($get_image != ''):
                    $image_properties = array(
                        'src' => 'assets/images/slide/'.$get_image,
                        'alt' => '',
                    );
?>
                <li><?php echo img($image_properties); ?></li>
                <?php else: ?>
                <li>No Slide</li>
<?php
                endif;
            endforeach;
        }       
?>

            </ul>
        </div> 
  • 我是否正确的循环结果在View或我应该这样做 控制器?
  • 我应该在活动记录或绑定(其中id =?)之间使用哪个查询,或者他们在差异情况下有自己的好处?

1 个答案:

答案 0 :(得分:0)

视图中的循环是正确的。因为它是“视图逻辑”(重复li),您可以使用到视图中。

您的模型可以更好。我个人喜欢使用该模型作为数据库中项目的表示。像这样:

class M_Headers extends CI_Model 
{
    private var $tablename = "tbl_slide";

    var $id;
    var $sl_image;

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

    public function get_all_slide()
    {
        $slides = array();
        $query = $this->db->get($this->tablename);
        foreach($query->result() as $row)
        {
             $item = new self();
             $item->id = $row->id;
             $item->sl_image = $row->sl_image;
             $slides[] = $item;
        }
        return $slides;
    }

    public function get_slide($id)
    {
        $this->db->where("id", $id);
        $query = $this->db->get($this->tablename);
        $results = $query->result();
        if(isset($results[0]))
        {
             $row = $results[0];
             $this->id = $row->id;
             $this->sl_image = $row->sl_image;
             return true;
        }
        return false;
    }
}

您甚至可以改进这一点,以创建一个将行加载到模型中的方法:

class M_Headers extends CI_Model 
{
    private var $tablename = "tbl_slide";

    var $id;
    var $sl_image;

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

    private function load_with_record($row)
    {
        $this->id = $row->id;
        $this->sl_image = $row->sl_image;
    }

    public function get_all_slide()
    {
        $slides = array();
        $query = $this->db->get($this->tablename);
        foreach($query->result() as $row)
        {
             $item = new self();
             $item->load_with_row($row);
             $slides[] = $item;
        }
        return $slides;
    }

    public function get_slide($id)
    {
        $this->db->where("id", $id);
        $query = $this->db->get($this->tablename);
        $results = $query->result();
        if(isset($results[0]))
        {
             $row = $results[0];
             $this->load_with_row($row);
             return true;
        }
        return false;
    }
}