简体   繁体   English

Codeigniter 3自动加载控制器

[英]Codeigniter 3 autoload controller

I'm using REST Server in codeigniter, and the way to use is that then in my app in all my controllers I must write this line on start: 我在codeigniter中使用REST服务器 ,使用方法是在我的应用程序中所有控制器中我必须在开始时写这行:

require APPPATH . '/libraries/REST_Controller.php';

Does anyone know how to autoload this REST_Controller and to avoid this line in all my controllers? 有谁知道如何自动加载这个REST_Controller并避免在我的所有控制器中使用这一行? I don't want to use require. 我不想使用require。

Thank in advance 预先感谢

You can achieve this through Codeigniter 's autoload configuration. 您可以通过Codeigniter的自动加载配置来实现此目的。

Edit your project's autoload.php which is located in directory YourProject/application/config/ 编辑项目的autoload.php ,它位于目录YourProject/application/config/

$autoload['libraries'] = array('REST_Controller');

And in controllers access this library class through $this->rest_controller . 并且在控制器中通过$this->rest_controller访问此库类。

BTW: Rest_Controllers is a library file, so I don't think a name suffixed with Controller is a good name for it. 顺便说一句:Rest_Controllers是一个库文件,所以我不认为用Controller后缀的名字是一个好名字。

Edit 编辑

Through your comment I got that you actually mean all of your controllers extended from REST_Controller , and you don't want require it at the top of every controller file. 通过你的评论,我知道你实际上是指从REST_Controller扩展的所有控制器,并且你不希望它在每个控制器文件的顶部。

Solution: 解:

  1. Move REST_Controller.php into directory YourProject/application/core/ . REST_Controller.php移动到目录YourProject/application/core/
  2. In YourProject/application/config/config.php line 119 change $config['subclass_prefix'] = 'MY_'; YourProject/application/config/config.php第119行中,更改$config['subclass_prefix'] = 'MY_'; to $config['subclass_prefix'] = 'REST_'; to $config['subclass_prefix'] = 'REST_';

then Codeigniter will load REST_Controller automatically. 然后Codeigniter将加载REST_Controller自动。

But the subclass_prefix config has a global effect, and you need change the location of REST_Conttoller.php , so to make minimal change I think the best way is you create MY_Controller class in directory ./application/core/ and require REST_Controller at bottom of this new file. 但是subclass_prefix配置有一个全局效果,你需要改变REST_Conttoller.php的位置,所以为了做出最小的改变,我认为最好的方法是在目录./application/core/创建MY_Controller类,并在底部需要REST_Controller新文件。 When CI load MY_controller automatically REST_Controller will be required too. CI加载MY_controller自动也需要REST_Controller

Notice: MY_Controller need extending from CI_Controller 注意: MY_Controller需要从CI_Controller扩展

Put file include in constructor of MY_Controller class, then extend it to any controller that needs to use REST_Controller . 将文件包含在MY_Controller类的构造函数中,然后将其扩展到需要使用REST_Controller任何控制器。 If you don't have MY_Controller.php file in APPPATH.'core/' location, make one and use it as presented here: 如果您在APPPATH.'core/'位置没有MY_Controller.php文件,请创建一个并使用它,如下所示:

<?php defined('BASEPATH') OR exit('See you next time.');

//APPPATH.'core/' location
class MY_Controller extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();

        require APPPATH . 'libraries/REST_Controller.php';//this constant ends with slash already
    }
}

Now, in every controller you want to use REST_Controller have code like this: 现在,在你想要使用的每个控制器中,REST_Controller都有这样的代码:

<?php defined('BASEPATH') OR exit('See you next time.');

//example controller
class Api extends MY_Controller
{
    public function __construct()
    {
        parent::__construct();
    }

    //bare example method
    public function some_get()
    {
        echo '<pre>', var_dump('Some REST_Controller code logic here'), '</pre>';
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM