0

我正在使用 CakePHP 2.x,并在 IIS 上本地运行它。我正在按照CakePHP方式将数据导出到CSV的教程进行操作,但出现错误。

我输入的 URL 是这样的:http://myproject.localhost/territory/spreadsheet.csv

我有Router::parseExtensions('csv');作为第一件事app\Config\routes.php

这是我的控制器:

`class 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.

我究竟做错了什么?

4

1 回答 1

1

这个不对

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

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

app\View\Territory\csv\spreadsheet.ctp

于 2012-06-04T07:59:04.023 回答