cakephp render-false动作仍然回显html模板 [英] cakephp render-false action still echoes html template

查看:205
本文介绍了cakephp render-false动作仍然回显html模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我的控制器中不需要视图的操作,我将禁用布局和模板,如下所示:

  $ this-> autoRender = false; 

这一切都很好。然而,在同一个动作中,我回应一个通过或失败来表示我的看法的结果。问题是一堆文本也回声:(我的失败或通过在最后)

 < ;! -  
要更改此模板,请选择工具|模板
并在编辑器中打开模板。
- >
<!DOCTYPE html>
< html>
< head>
< meta http-equiv =Content-Typecontent =text / html; charset = UTF-8>
< title>< / title>
< / head>
< body>
< / body>
< / html>
<! -
要更改此模板,请选择工具|模板
并在编辑器中打开模板。
- >
<!DOCTYPE html>
< html>
< head> ....

这是回应8 o 9次。



如何摆脱这样的只有我的通过或失败回声?
您能帮助吗?



我尝试过

  $ this-> layout = false;和
$ this-> render(false);非常感谢你。



em> 更新:**
只是注意到它也回荡了一堆javascrip代码,无处可见(删除< for pasting here)eg:pre class =cake -errora href =javascript:void(0); onclick =document.getElementById('cakeErr5035af14add0c-trace')。style.display =(document.getElemen ..



是整个操作**

  //此操作通过以下方式调用:
// mysite / qcas / loadProdFromFile / dirId:76
//或
// mysite / qcas / loadProdFromFile / dirId:76 / filePath:J:\ep12219 - Air Pollution\Load\prodValues.csv


function loadProdFromFile(){
$ this-> autoRender = false;

//这里我们得到dir信息)param received:dirId
$ dirName = $ this-> Qca-> Dir-> find('first',array(
'recursive'=> 0,
' fields'=> array('Dir.name'),
'conditions'=> array('Dir.id'=> $ this-> request-& 'dirId']),

);

//如果使用没有提供filePath param,我们使用默认位置基于dir info
if (is_null($ this-> request-> params ['named'] ['filePath']))){
$ basedir ='/ disk / main / jobs /';
$ dirs = scandir($ basedir);

$ found = 0;
foreach($ dirs as $ key => $ value){
if(strpos($ value,$ dirName ['Dir'] ['name'])> -1){
$ found = 1;
break;
}
}
if(!$ found){
echo'failfile';
exit;
}
$ loadDir ='/ disk / main / jobs /'。 $ value。 '/加载/';
$ thefiles = glob($ loadDir。* .csv);
$ prodFile = $ thefiles [0];
} else {
//如果用户提供了一个路径,我们构建一个unix路径
//因为某种原因扩展没有发布,所以我们添加它:只有csv可以被处理
$ prodFile = AppController :: BuildDirsFile($ this-> request-> params ['named'] ['filePath'])。 .csv;
}

//我们在这里得到一个工作文件路径
$ fileHandle = fopen($ prodFile,'r');

if($ fileHandle){
//开始处理文件以构建$ prodata数组以保存到db
$ counter = 0;
while(!feof($ fileHandle)){
$ line = fgets($ fileHandle);
if(strlen($ line)== 0){
break;
}

$ values = explode(',',$ line);
$ prodata [$ counter] ['dir_id'] = $ this-> request-> params ['named'] ['dirId'];
$ prodata [$ counter] ['name'] = $ dirName ['Dir'] ['name'];
$ prodata [$ counter] ['employee_id'] = $ values [1];

$ a = strptime($ values [0],'%m /%d /%Y');
$ timestamp = mktime(0,0,0,$ a ['tm_mon'] + 1,$ a ['tm_mday'],$ a ['tm_year'] + 1900)
$ prodata [$ counter] ['qca_start'] = $ timestamp;

$ end = $ timestamp +($ values [2] * 60);
$ prodata [$ counter] ['qca_end'] = $ end;

$ prodata [$ counter] ['qca_tipcode'] = $ values [3] * -1;

$ prodata [$ counter] ['qca_durint'] = 0;
$ prodata [$ counter] ['qca_durtel'] = 0;
$ prodata [$ counter] ['qca_durend'] = 0;
$ prodata [$ counter] ['qca_signal'] = 0;
$ prodata [$ counter] ['qca_restart'] = 0;
$ prodata [$ counter] ['qca_stop'] = 0;
$ prodata [$ counter] ['qca_prevtipc'] = 0;
$ prodata [$ counter] ['qca_respid'] = 0;
$ prodata [$ counter] ['qca_lastq'] = 0;
$ prodata [$ counter] ['qca_smskey'] = 0;
$ prodata [$ counter] ['qca_telconta'] = 0;
$ prodata [$ counter] ['qca_execuqoc'] = 0;
$ counter ++;
}
} else {
//文件不好
echo'fail';
exit;
}
if(count($ prodata)> 0){
$ this-> Qca-> saveMany($ prodata);
echo count($ prodata);
}
}


解决方案

我将在这里粘贴一个来自cakeapi的片段,位于 http://api.cakephp.org/class/controller 关于autoRender如何工作:



autoRender boolean

设置为true以自动呈现视图之后



因此:

  $ this-> autoRender =假的

通常会关闭呈现视图,但只是在动作逻辑完成后。所以这就是为什么你从你的行动逻辑得到回报。您可以选择是否从代码中删除回声,以防止显示它们或尝试此模式。



如果出现错误:

  $ this-> Session-> setFlash('Error'); 
$ this-> redirect(array('action'=>'your_error_page'));

这将移动到ur错误页面,错误字符串作为Flash文本, :

  $ this-> Session-> setFlash('Its fine dude!'); 
$ this-> redirect(array('action'=>'your_ok_page'));


For an action in my controller that needs no view, I'm disabling the layout and template like this:

$this->autoRender = false;

And it's all good. However, in that same action I'm echoing a 'pass' or 'fail' to signal my view of the result. The problem is a bunch of text is also echoed: (my 'fail' or 'pass' at the very end)

 <!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE html>
<html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title></title>
</head>
<body>
        </body>
</html>
<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE html>
<html>
    <head> ....

This is echoed 8 o 9 times.

How can I get rid of that so only my 'pass' or 'fail' are echoed? Can you help?

I've tried

$this->layout = false; and
$this->render(false);

Thank you so much.

Update:** Just noted it also echoes a bunch of javascrip code out of nowhere, (Removed < for pasting here) e.g.: pre class="cake-error" a href="javascript:void(0);" onclick="document.getElementById('cakeErr5035af14add0c-trace').style.display = (document.getElemen..

This is the whole action **

//This action is called via:
//mysite/qcas/loadProdFromFile/dirId:76
// or
//mysite/qcas/loadProdFromFile/dirId:76/filePath:J:\ep12219 - Air Pollution\Load\prodValues.csv


function loadProdFromFile() {
    $this->autoRender = false;

    // here we get dir info based on first (and perhaps sole) param received: dirId
    $dirName = $this->Qca->Dir->find('first', array(
        'recursive' => 0,
        'fields' => array('Dir.name'),
        'conditions' => array('Dir.id' => $this->request->params['named']['dirId']),
            )
    );

    //if used did not provide filePath param, we use a default location based on dir info
    if ((is_null($this->request->params['named']['filePath']))) {
        $basedir = '/disk/main/jobs/';
        $dirs = scandir($basedir);

        $found = 0;
        foreach ($dirs as $key => $value) {
            if (strpos($value, $dirName['Dir']['name']) > -1) {
                $found = 1;
                break;
            }
        }
        if (!$found) {
            echo 'failfile';
            exit;
        }
        $loadDir = '/disk/main/jobs/' . $value . '/Load/';
        $thefiles = glob($loadDir . "*.csv");
        $prodFile = $thefiles[0];
    } else {
        // if user provided a path, we build a unix path
        // for some reason the extension is not posted, so we append it: only csv can be processed anyways
        $prodFile = AppController::BuildDirsFile($this->request->params['named']['filePath']) . ".csv";
    }

    // we get here with a working file path
    $fileHandle = fopen($prodFile, 'r');

    if ($fileHandle) {
        // start processing file to build $prodata array for saving to db
        $counter = 0;
        while (!feof($fileHandle)) {
            $line = fgets($fileHandle);
            if (strlen($line) == 0) {
                break;
            }

            $values = explode(',', $line);
            $prodata[$counter]['dir_id'] = $this->request->params['named']['dirId'];
            $prodata[$counter]['name'] = $dirName['Dir']['name'];
            $prodata[$counter]['employee_id'] = $values[1];

            $a = strptime($values[0], '%m/%d/%Y');
            $timestamp = mktime(0, 0, 0, $a['tm_mon'] + 1, $a['tm_mday'], $a['tm_year'] + 1900);
            $prodata[$counter]['qca_start'] = $timestamp;

            $end = $timestamp + ($values[2] * 60);
            $prodata[$counter]['qca_end'] = $end;

            $prodata[$counter]['qca_tipcode'] = $values[3] * -1;

            $prodata[$counter]['qca_durint'] = 0;
            $prodata[$counter]['qca_durtel '] = 0;
            $prodata[$counter]['qca_durend'] = 0;
            $prodata[$counter]['qca_signal'] = 0;
            $prodata[$counter]['qca_restart'] = 0;
            $prodata[$counter]['qca_stop'] = 0;
            $prodata[$counter]['qca_prevtipc'] = 0;
            $prodata[$counter]['qca_respid'] = 0;
            $prodata[$counter]['qca_lastq'] = 0;
            $prodata[$counter]['qca_smskey'] = 0;
            $prodata[$counter]['qca_telconta'] = 0;
            $prodata[$counter]['qca_execuqoc'] = 0;
            $counter++;
        }
    } else {
        // file was just no good
        echo 'fail';
        exit;
    }
    if (count($prodata) > 0) {
        $this->Qca->saveMany($prodata);
        echo count($prodata);
    }
}

解决方案

First of all I will paste here a fragment from cakeapi under http://api.cakephp.org/class/controller about how autoRender works:

autoRender boolean
Set to true to automatically render the view after action logic.

So this:

 $this->autoRender = false ;

will generally turns off rendering the view but just after action logic is done. So thats why you get the echos from your action logic. You can whether remove your echo's from your code to prevent showing them or try this schema,

if something is wrong:

$this->Session->setFlash('Error');
$this->redirect(array('action' => 'your_error_page'));

which will move you to ur error page with Error string as a Flash text, or when its all fine:

$this->Session->setFlash('Its fine dude!');
$this->redirect(array('action' => 'your_ok_page'));

这篇关于cakephp render-false动作仍然回显html模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆