ci 3.03 アプリケーションで restful を使用したい:
- このtutplusチュートリアルを見つけました
- ファイルをダウンロードして
codeigniter-restserver-master.zip
ファイルをコピーFormat.php
しREST_Controller.php(@version 3.0.0)
ました/application/libraries/REST directory
コントロール application/controllers/api/Users.php を作成しました:
require_once("application/libraries/REST/REST_Controller.php"); require_once("application/libraries/REST/Format.php"); class Users extends REST_Controller { //protected $rest_format = 'json'; function users_get() { //$users = $this->user_model->get_all(); $filter_username= $this->get('filter_username'); $filter_user_group= $this->get('filter_user_group'); $filter_active= $this->get('filter_active'); $sort= $this->get('sort'); $sort_direction= $this->get('sort_direction'); //, $filter_user_group, $filter_active, $sort, $sort_direction $users_list = $this->muser->getUsersList(false, ''/*, $filter_username, $filter_user_group, $filter_active, $sort, $sort_direction, ''*/); echo '<pre>'.count($users_list).'::$users_lists::'.print_r($users_list,true).'</pre>'; if($users_list) { $this->response($users, 200); } else { $this->response(NULL, 404); } }
URL http://local-ci3.com/api/usersを実行すると、多くのエラーが発生しました。
PHP エラーが発生しました
Severity: Notice
Message: Undefined property: Users::$format
Filename: REST/REST_Controller.php
Line Number: 734
Backtrace:
File: /mnt/diskD_Work/wwwroot/ci3/application/libraries/REST/REST_Controller.php
Line: 734
Function: _error_handler
File: /mnt/diskD_Work/wwwroot/ci3/application/libraries/REST/REST_Controller.php
Line: 649
Function: response
File: /mnt/diskD_Work/wwwroot/ci3/index.php
Line: 292
Function: require_once
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Users::$format
Filename: REST/REST_Controller.php
Line Number: 752
Backtrace:
File: /mnt/diskD_Work/wwwroot/ci3/application/libraries/REST/REST_Controller.php
Line: 752
Function: _error_handler
File: /mnt/diskD_Work/wwwroot/ci3/application/libraries/REST/REST_Controller.php
Line: 649
Function: response
File: /mnt/diskD_Work/wwwroot/ci3/index.php
Line: 292
Function: require_once
実際、REST api の作成に役立つ実行可能なライブラリを取得したいと考えていました。ゼロから作るよりも、その方が好ましいと思います。しかし、このライブラリは機能しませんか、それとも修正が必要ですか? 申し訳ありませんが、このライブラリがci 2専用である場合、私が見逃したのは何ですか?
このフォーラムで検索を行ったところ、次のようなヒントが見つかりました。
Format.php と Rest_Controller.php の両方をコントローラーにロードすると、同じ問題が発生します。Format.php をざっと見てみると、スタンドアロンのフォーマット変換ヘルパーのように見えます。Rest_Controller.php をロードして、問題が解決するかどうかを確認してください。
ラインにコメントしました
//require_once("application/libraries/REST/Format.php");
私のコントローラーでは、まだ次のようなエラーが表示されます: Message: Undefined property: Users::$format. このライブラリのコードを確認しようとしましたが、データが json 形式に変換されると、731-757 行目に無効なブロックがあることを確認しました。
elseif ($data !== NULL)
{
// If the format method exists, call and return the output in that format
if (method_exists($this->format, 'to_' . $this->response->format))
{
// Set the format header
$this->output->set_content_type($this->_supported_formats[$this->response->format], strtolower($this->config->item('charset')));
$output = $this->format->factory($data)->{'to_' . $this->response->format}();
// An array must be parsed as a string, so as not to cause an array to string error
// Json is the most appropriate form for such a datatype
if ($this->response->format === 'array')
{
$output = $this->format->factory($output)->{'to_json'}();
}
}
else
{
// If an array or object, then parse as a json, so as to be a 'string'
if (is_array($data) || is_object($data))
{
$data = $this->format->factory($data)->{'to_json'}();
}
// Format is not supported, so output the raw data as a string
$output = $data;
}
}
このブロックにコメントしようとしたが、エラーが発生した場合
Message: Array to string conversion
この場合、データは変換されないようです...
これらのエラーを修正することは可能ですか? または、上記のライブラリと同様のインターフェイスを備えた codeigniter 3 REST API の実行可能なライブラリについてアドバイスをいただけますか?
ありがとう!