8

私はcodeigniter でREST サーバーを使用しています。使用方法は、アプリのすべてのコントローラーで、開始時に次の行を記述する必要があることです。

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

この REST_Controller を自動ロードし、すべてのコントローラーでこの行を回避する方法を知っている人はいますか? 私はrequireを使いたくありません。

少し早いですがお礼を

4

2 に答える 2

3

クラスのコンストラクターにファイル インクルードを配置しMY_Controller、それを使用する必要がある任意のコントローラーに拡張しますREST_ControllerMY_Controller.php場所にファイルがない場合は、ファイルをAPPPATH.'core/'作成して、次のように使用します。

<?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
    }
}

ここで、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>';
    }
}
于 2016-08-10T08:05:50.923 に答える