在Codeigniter中向控制器类添加后缀

时间:2014-05-17 01:12:49

标签: php codeigniter

我已经通过路由器类,但没有找到加载控制器类的位置。

我发现通常控制器类名称与模型具有相同的名称是有意义的,例如我可能有一个名为user的控制器以及一个名为user的模型,其中控制器具有基本的crud函数进入它。

问题是,如果我在User控制器中调用User模型,我就无法重新声明该类。

我建议将控制器名称设置为User_Controller,以免重新声明用户模型。

有谁知道如何实现这个或更好的解决方案?

1 个答案:

答案 0 :(得分:1)

我并不安静地理解你的问题,也许你正在寻找"命名"约定或使用load model第二个参数。请参阅下面的示例。

<强>控制器

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

class User extends CI_Controller {
    public function __construct() {
        parent::__construct();
        $this->load->model('user_model', 'user');
    }

    public function index() {
        $this->user->get(); //calls user_model method get();
    }

}

/* End of file user.php */
/* Location: ./application/controllers/user.php */

<强>模型

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

class User_model extends CI_Model {

    function get() {
        return 1; //all database calls etc..
    }

    function complexFunction() {
        $this->get(); //calls User_model get()
        return 1;
    }

}

/* End of file user_model.php */
/* Location: ./application/models/user_model.php */