【问题标题】:loading the model in the constructor function in codeigniter. is that good practice or no?在codeigniter的构造函数中加载模型。这是好习惯吗?
【发布时间】:2012-10-06 10:25:15
【问题描述】:

我想知道...我可以在我的构造函数中加载模型,而不是为该控制器中的每个函数加载它吗?

假设我有 5 个使用相同模型的函数。除了在所有 5 个函数中调用相同的模型,我应该像这样在构造函数中加载控制器吗?

这是好的做法吗?

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

class BackOfficeUsers extends MY_Controller 
{

    // constructor function 
    public function __construct() 
    {
        parent::__construct();
        $this->load->model('backOfficeUsersModel'); // load it here??
    } // end of constructor function

问候,卓然

【问题讨论】:

    标签: codeigniter


    【解决方案1】:

    是的,DRY 始终是一个好习惯。

    您也可以使用CodeIgniter's Autoloading feature。如果您发现自己在其他控制器中过于频繁地加载此模型。

    【讨论】:

    • 我同意,只需使用 autoload.php
    猜你喜欢
    • 2021-04-11
    • 2019-02-01
    • 2021-03-15
    • 2016-12-11
    • 1970-01-01
    • 1970-01-01
    • 2016-06-27
    • 1970-01-01
    • 2017-10-11
    相关资源
    最近更新 更多