4

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

$this->autoRender = false;

这一切都很好。然而,在同一个动作中,我正在呼应“通过”或“失败”以表明我对结果的看法。问题是还回显了一堆文本:(最后是我的“失败”或“通过”)

 <!--
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> ....

这被重复了 8 到 9 次。

我怎样才能摆脱这种情况,所以只有我的“通过”或“失败”才会得到回应?你能帮我吗?

我试过了

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

非常感谢。

更新:** 刚刚注意到它还突然出现了一堆 javascrip 代码,(已删除 < 用于粘贴此处)例如:pre class="cake-error" a href="javascript:void(0);" onclick="document.getElementById('cakeErr5035af14add0c-trace').style.display = (document.getElemen..

这是整个动作**

//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);
    }
}
4

1 回答 1

7

首先,我将在http://api.cakephp.org/class/controller下粘贴来自 cakeapi 的片段,了解 autoRender 的工作原理:

autoRender boolean设置为 true 以在操作逻辑之后
自动渲染视图。

所以这:

 $this->autoRender = false ;

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

如果有问题:

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

这会将您移动到带有错误字符串作为 Flash 文本的错误页面,或者当一切正常时:

$this->Session->setFlash('Its fine dude!');
$this->redirect(array('action' => 'your_ok_page'));
于 2012-09-24T12:28:53.233 回答