###CakePHP3にて
該当コントローラーのアクション内にて$this->autoRender = false; を指定すると、viewファイルによる描画だけでなくecho "test"の内容も表示されません。
どなたかご教授お願い致します。
問題解決のためにできる限りシンプルな内容で作成しました。
以下、行ったことの一覧です。
- 「php composer.phar create-project --prefer-dist cakephp/app testapp」コマンドにて新しいプロジェクトを作成。
- mysql上に'testapp'データベースを作成。
- app.confのDatasources部を修正。
- 手書きにてTestsController.phpを作成。
- 表示を確認。
以下該当コードです。
php
1<?php 2 'Datasources' => [ 3 'default' => [ 4 'className' => 'Cake\Database\Connection', 5 'driver' => 'Cake\Database\Driver\Mysql', 6 'persistent' => false, 7 'host' => 'localhost', 8 /** 9 * CakePHP will use the default DB port based on the driver selected 10 * MySQL on MAMP uses port 8889, MAMP users will want to uncomment 11 * the following line and set the port accordingly 12 */ 13 //'port' => 'non_standard_port_number', 14 'username' => 'testapp', 15 'password' => 'testapp', 16 'database' => 'testapp', 17 'encoding' => 'utf8', 18 'timezone' => 'UTC', 19 'flags' => [], 20 'cacheMetadata' => true, 21 'log' => false, 22 23 /** 24 * Set identifier quoting to true if you are using reserved words or 25 * special characters in your table or column names. Enabling this 26 * setting will result in queries built using the Query Builder having 27 * identifiers quoted when creating SQL. It should be noted that this 28 * decreases performance because each query needs to be traversed and 29 * manipulated before being executed. 30 */ 31 'quoteIdentifiers' => false, 32 33 /** 34 * During development, if using MySQL < 5.6, uncommenting the 35 * following line could boost the speed at which schema metadata is 36 * fetched from the database. It can also be set directly with the 37 * mysql configuration directive 'innodb_stats_on_metadata = 0' 38 * which is the recommended value in production environments 39 */ 40 //'init' => ['SET GLOBAL innodb_stats_on_metadata = 0'], 41 42 'url' => env('DATABASE_URL', null), 43 ],
php
1<?php 2namespace App\Controller; 3 4use App\Controller\AppController; 5 6class TestsController extends AppController 7{ 8 public function index() { 9 $this->autoRender = false; 10 echo "test"; 11 } 12} 13
よろしくお願い致します。
追記:
ご提示いただいたコードを参考にコードを作成しましたが、ブラウザよりアクセスすると「Controller actions can only return Cake\Network\Response or null.」というメッセージが表示され、
ajax経由でアクセスすると「500 (Internal Server Error)」というメッセージが帰ってきます。
ご教授お願い致します。
php
1<?php 2namespace App\Controller; 3 4use App\Controller\AppController; 5 6class TestsController extends AppController 7{ 8 public function index() { 9 $this->autoRender = false; 10 $returnData = array('test'=>'test'); 11 return json_encode($returnData); 12 } 13}