###前提・実現したいこと
CakePHP3でRESTfulなAPIを構築したいと思っています。
下の公式ドキュメントを参照しながら作っています。
https://book.cakephp.org/3.0/ja/development/rest.html
https://book.cakephp.org/3.0/ja/development/routing.html#resource-routes
ここで、2つのモデルWindspeedとWinddegreeをシリアライズして、WindsController.phpというコントローラー(Windモデルは存在しない)からJSONレスポンスを返そうとしています。
###発生している問題・エラーメッセージ
しかしながら以下のようなエラーが発生してしまいます。
Fatal error: Call to a member function find() on boolean in /vagrant/public_html/src/Controller/WindsController.php on line 33
どうやら$this->Windspeed->find()でエラーが起きているようですが、
public $uses = array('Windspeed', 'Winddegree');を指定しても、$this->Windspeed、$this->Winddegreeが存在していない上、var_dump($this->Winds)の結果から
object(Cake\ORM\Table)[96] public 'registryAlias' => string 'Winds' (length=5) public 'table' => string 'winds' (length=5) public 'alias' => string 'Winds' (length=5) public 'entityClass' => string '\Cake\ORM\Entity' (length=16) public 'associations' => array (size=0) empty public 'behaviors' => array (size=0) empty public 'defaultConnection' => string 'default' (length=7) public 'connectionName' => string 'default' (length=7)
public $uses = array('Windspeed', 'Winddegree');を指定すれば、命名規則によって自動的にWindを探さないはず?だと思ったのですが、探してきている上に、こういった場合$this->Windsではなく$this->Windで取得できるはずでは?という疑問もあります。
WindsController.php
class WindsController extends AppController { public $uses = array('Windspeed', 'Winddegree'); public function initialize() { parent::initialize(); $this->loadComponent('RequestHandler'); } public function index() { $windspeeds = $this->Windspeed->find('all', array( 'order' => 'Model.get_time DESC', 'limit' => '24' ) ); $winddegrees = $this->Winddegree->find('all', array( 'order' => 'Model.get_time DESC', 'limit' => '24' ) ); $this->set([ 'windspeeds' => $windspeeds, 'winddegrees' => $winddegrees, '_serialize' => ['windspeeds', 'winddegrees'] ]); } ]);
仮に
public $uses = array('Windspeed', 'Winddegree');
でのモデルの指定ではなく、
public $uses = array();
としたのちに、
$this->loadModel('Windspeeds');
$this->loadModel('Winddegrees');
としてあげれば$this->Windspeedsなどとして使えるのですが、
ここに関してもテーブル名がwindspeeds,winddegreesならば命名規則によれば
$this->Windspeedとして使えなければならないのではないか?という疑問があります。
また、仮に$this->Windspeeds->find('all');として結果を取得できた場合でも、
index.ctpが見つからないというエラーが発生してしまいます。
RESTfulなAPIの時にはビューファイルは必要ではないのではなかったでしょうか?
routes.phpの設定は以下です。
Router::defaultRouteClass(DashedRoute::class); Router::scope('/', function ($routes) { $routes->fallbacks(DashedRoute::class); }); Router::scope('/api', function ($routes) { $routes->extensions(['json']); $routes->resources('Winds', [ 'only' => ['index', 'view'] ]); });
###補足情報(言語/FW/ツール等のバージョンなど)
PHP 5.6.30 (cli) (built: Jan 19 2017 08:09:42) Copyright (c) 1997-2016 The PHP Group Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2016, by Zend Technologies with Xdebug v2.5.0, Copyright (c) 2002-2016, by Derick Rethans
mysql Ver 14.14 Distrib 5.6.35, for Linux (x86_64) using EditLine wrapper
CakePHP 3.3.12
Vagrant、Apache(httpd)上で起動
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/01/29 14:23