Prefill字段CodeIgniter类

时间:2013-12-26 18:25:53

标签: php codeigniter function class

我在CodeIgniter中创建控制器,需要在数据库中存储一些设置。 目前我这样做:

public function index()
{
    $config["function"] = __FUNCTION__;
    $config["class"]    = __CLASS__;
    $config["page"]     = "homepage";
    $this->core->initialize($config);
}

在Core库中保存这些配置。 如你所见,我使用PHP的__ FUNCTION __和__ CLASS __将它们存储到$ config中。

有没有办法自动化这个过程?因此,每当我创建一个新的控制器时,字段$ config [“class”]& $ config [“function”]会自动填写吗?

1 个答案:

答案 0 :(得分:0)

您可以尝试扩展CI_Controller并让您拥有这样的&把它放在application/core/目录中:

class MY_Controller extends CI_Controller
{
    function __construct()
    {
        $config["function"] = __FUNCTION__;
        $config["class"]    = __CLASS__;
        $config["page"]     = "homepage";
        $this->core->initialize($config);

        parent::__construct();
    }
}

然后每个控制器都会扩展MY_Controller而不是CI_Controller,如下所示:

class ExampleController extends MY_Controller
{
    //if you have your own __construct method you need to
    //make sure that parent::__construct(); is present
    function __construct()
    {
        //other code...

        parent::__construct();
    }
}

您可以阅读有关在http://ellislab.com/codeigniter%20/user-guide/general/core_classes.html

扩展CI核心课程的信息

祝你的项目好运

<强> ---------- --------- EDIT

我不确定$this->core是否能正常工作,因为在您的示例中没有任何设置此变量的内容。您可能需要在ExampleController类中进行设置。

相关问题