问题描述
我试图使用自定义 CakeResponse
类,但是我当前的工作或当前的搜索都没有导致我实际使用一个解决方案。
所以让我们调用我的自定义 CakeResponse
class AppResponse
:
AppResponse
位于 Lib / Network
<?php
App :: uses('CakeResponse','Network');
class AppResponse extends CakeResponse {
}
博客文章,似乎应该能够使用此代码覆盖CakeResponse并使用自定义Response类
App :: import('Lib','CustomResponse');
类AppController extends Controller {
protected $ _responseClass ='CustomResponse';
}
下面的
语句不起作用
App :: uses('AppResponse','Network');
App :: uses('AppResponse','Lib / Network');
App :: uses('AppResponse','Lib');
App :: import('Lib','AppResponse');
类AppController extends Controller {
protected $ _responseClass ='AppResponse';
}
public function beforeRender(){
debug($ this-> _responseClass);
debug(get_class($ this-> response));
die();
}
这将输出:
AppResponse
CakeResponse
图书和似乎暗示可以覆盖CakeResponse类
如上所述,它似乎不起作用。 >解决方案
解决方案是编辑 webroot / index.php
文件,并更改用于您的新的自定义CakeResponse类的响应类。
解决方案:
App :: uses('AppResponse' 'Lib / Network');
$ Dispatcher = new Dispatcher();
$ Dispatcher-> dispatch(
new CakeRequest(),
new AppResponse()
);
注意:此解决方案的折扣。
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.
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.
这篇关于在CakePHP 2.1+中使用自定义CakeResponse类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!