cakephp render-false动作仍然回应html模板

时间:2012-08-21 19:56:29

标签: cakephp layout

对于我的控制器中不需要查看的操作,我正在禁用这样的布局和模板:

$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代码,(删除&lt; for pasting here)例如: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);
    }
}

1 个答案:

答案 0 :(得分:6)

首先,我将在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'));