输出CSV时缺少查看文件

时间:2012-06-04 01:41:31

标签: cakephp csv routing

我正在使用CakePHP 2.x,并在IIS上本地运行它。我正在关注Exporting data to CSV the CakePHP way上的教程,我收到了一个错误。

我输入的网址如下:http://myproject.localhost/territory/spreadsheet.csv

Router::parseExtensions('csv');

中的第一件事是app\Config\routes.php

这是我的控制器:

`类TerritoryController扩展AppController {     public $ useTable ='ezrep_territory';

public $paginate = array('limit' => 50);

public function beforeFilter()
{
    parent::beforeFilter();
    $this->Auth->deny('index');
}

public function Index()
{
    // ... snip ...
}

public function Spreadsheet()
{
    $data = $this->Territory->find(
        'all',
        array(
            'conditions' => array('Territory.ez' => $this->ez),
            'fields' => array('territory','terrcode','terrdesc'),
            'contain' => false
    ));
    $headers = array(
        'Territory'=>array(
            'territory' => 'Territory ID',
            'terrcode' => 'Terr Code',
            'terrdesc' => 'Terr Desc'
        )
    );

    array_unshift($data,$headers);
    $this->set(compact('data'));
}

} `

app\View\Layouts\csv中,我有一个文件default.ctp

<?php echo $content_for_layout; ?>

app\View\Territory中,我有一个文件spreadsheet.ctp

// Loop through the data array
foreach ($data as $row)
{
    // Loop through every value in a row
    foreach ($row['Territory'] as &$value)
    {
        // Apply opening and closing text delimiters to every value
        $value = "\"".$value."\"";
    }
    // Echo all values in a row comma separated
    echo implode(",",$row['Territory'])."\n";
}

当我转到http://myproject.localhost/territory/spreadsheet.csv时,我会看到一个似乎通过app\View\Layouts\default.ctp呈现错误的页面:

View file &quot;C:\zproj\ezrep40\app\View\Territory\csv\spreadsheet.ctp&quot; is missing.Error: An Internal Error Has Occurred.

我做错了什么?

1 个答案:

答案 0 :(得分:1)

这不对

  

在app \ View \ Territory中,我有一个文件spreadsheet.ctp:

您还需要将视图(而不仅仅是布局)放在以扩展名命名的子目录中:

  

应用\视图\领土\ CSV \ spreadsheet.ctp