问题描述
我正在使用cakePHP2.x。我正在尝试使用空白布局渲染视图。该视图应仅包含从动作控制器返回的字符串,且没有html代码。
这是代码:
I'm using cakePHP 2.x. I'm trying to render a view with an empty layout. The view should contain only a string returned from the action controller with no html code.Here is the code:
public function checkout(){
$ref =null;$act=null;$par=null;
$this->layout = false;
//$this->render(false);
//$priceCTP = $this->Session->read('priceCTP');;
$priceCTP = $this->getPrice();
//var_dump(session_save_path());
if (isset($this->params['url']['Reference'])) {
$ref = $this->params['url']['Reference'];
}
if (isset($this->params['url']['Action'])) {
$act = $this->params['url']['Action'];
}
if (isset($this->params['url']['Param'])) {
$par = $this->params['url']['Param'];//$_GET['Param'];//this->request->params['named']
}
switch ($act) {
case "DETAIL":// accéder à la base et récuperer le montant
echo "Reference=".$ref."&Action=".$act."&Reponse=".$priceCTP;
break;
case "ERREUR": // accéder à la base et mettre à jour l’état de la transaction
echo "Reference=".$ref. "&Action=".$act. "&Reponse=OK";
break;
case "ACCORD": // accéder à la base, enregistrer le numéro d’autorisation (dans param)
echo "Reference=".$ref. "&Action=".$act. "&Reponse=OK";
break;
case "REFUS": // accéder à la base et mettre à jour l’état de la transaction
echo "Reference=".$ref. "&Action=".$act. "&Reponse=OK";
break;
case "ANNULATION": // accéder à la base et mettre à jour l’état de la transaction
echo "Reference=".$ref. "&Action=".$act. "&Reponse=OK";
break;
}
}
此url上的结果(?
Reference=123&Action=DETAIL&Param=)
"Reference=123&Action=DETAIL&Repon"
rather than
"Reference=123&Action=DETAIL&Reponse=121,630" (works fine on localhost).
推荐答案
不要从控制器中回显数据
如注释中所述,控制器动作不应回显数据,即使它在某些情况下(甚至在大多数情况下)都可以工作。输出数据的正确方法是使用(数据)视图,配置并返回响应对象,甚至返回字符串(但是与CakePHP的未来版本不向前兼容)。
Do not echo data from controllers
As mentioned in the comments, controller actions are not supposed to echo data, even though it might work in some, maybe even most situations. The correct way of outputting data is by using (data) views, by configuring and return the response object, or even returning a string (which however isn't forward compatible with future versions of CakePHP).
回送数据会导致各种问题,从测试环境中无法识别的数据到无法发送标头,甚至是切断数据,这都是您遇到的问题。例如,当 Content-Length
标头中发送的大小与发送的数据的实际大小不匹配时,可能会发生后者,这通常发生在以下情况下:
Echoing data can lead to all kinds of problems, from the data not being recognized in the test environment, to headers not being able to be sent, and even data being cut off, which is what you are experiencing. The latter can for example happen when the size sent in the Content-Length
header doesn't match the actual size of the data being sent, this often happens in situations where the content is being gzipped.
如果遇到了gzipped响应问题的长度不匹配,那么您必须弄清楚压缩的位置(在服务器级别(例如 mod_deflate
),在PHP扩展级别(例如 zlib $ c) $ c>),在PHP代码级别(例如
CakeResponse :: compress()
)),以及为什么它没有相应地设置适当的长度。
If you've run into the length mismatch for gzipped responses problem, then you have to figure out where compression is being involved (on server level (for example mod_deflate
), on PHP extension level (for example zlib
), on PHP code level (for example CakeResponse::compress()
)), and why it doesn't set the proper length accordingly.
也就是说,只需将所需的数据传递到视图,然后从那里构建并回显响应数据。鉴于您不想使用HTML进行响应,您可能还需要相应地设置响应类型:
That being said, just pass the required data to the view, and build and echo the reponse data from there. Given that you don't want to respond with HTML, you probably also want to set the response type accordingly:
public function checkout()
{
$this->response->type('text'); // = text/plain
// ...
$this->set(compact(array('priceCTP', 'ref', 'act', 'par')));
}
如上所述,另一种选择是返回适当的响应对象:
As mentioned, another option would be to return a proper response object:
public function checkout()
{
$this->response->type('text');
// ...
$body = "Reference=" . $ref . "&Action=" . $act . "&Reponse=";
switch ($act) {
case "DETAIL":
$body .= $priceCTP;
break;
case "ERREUR":
case "ACCORD":
case "REFUS":
case "ANNULATION":
$body .= "OK";
break;
// ...
}
// ...
$this->response->body($body);
return $this->response;
}
ps。您真的想用法文的钥匙回应吗? 响应
可能应该是响应
。
ps. do you really want to respond with a key in french? Reponse
maybe should be Response
.
pps。假设您使用查询字符串进行响应,则可能希望/需要对值进行URL编码。
pps. given that you respond with a query string, you may want/need to URL encode the values accordingly.
另请参见
- Cookbook > Controllers > Controller actions
- Cookbook > Controllers > Request > Request and Response objects > CakeResponse > Dealing with content types
这篇关于回声字符串无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!