问题
I am trying to use a Custom CakeResponse
class however none of my current work nor current searches have led me to a solution towards actually using one.
So let's call my custom CakeResponse
class AppResponse
:
AppResponse
is in Lib/Network
<?php
App::uses('CakeResponse', 'Network');
class AppResponse extends CakeResponse {
}
Based on this blog post http://mark-story.com/posts/view/cakeresponse-in-cakephp-2-0 it appears that one should be able to use this code to override the CakeResponse and use the custom Response class
App::import('Lib', 'CustomResponse');
class AppController extends Controller {
protected $_responseClass = 'CustomResponse';
}
The below collection of App::uses/import
statements does not work
App::uses('AppResponse', 'Network');
App::uses('AppResponse', 'Lib/Network');
App::uses('AppResponse', 'Lib');
App::import('Lib', 'AppResponse');
class AppController extends Controller {
protected $_responseClass = 'AppResponse';
}
public function beforeRender() {
debug($this->_responseClass);
debug(get_class($this->response));
die();
}
This will output:
AppResponse
CakeResponse
The Book and CakePHP docs do seem to suggest it is possible to overwrite a CakeResponse class and use your own custom one.
As evidenced by the above, it doesn't appear to work.
回答1:
Solution to this is to edit the webroot/index.php
file and change the Response Class used to your new custom CakeResponse Class.
Solution:
App::uses('AppResponse', 'Lib/Network');
$Dispatcher = new Dispatcher();
$Dispatcher->dispatch(
new CakeRequest(),
new AppResponse()
);
Note: Credit for this solution is a tweet from Mark Story.
来源:https://stackoverflow.com/questions/28488762/use-a-custom-cakeresponse-class-in-cakephp-2-1