我正在使用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 "C:\zproj\ezrep40\app\View\Territory\csv\spreadsheet.ctp" is missing.
和
Error: An Internal Error Has Occurred.
我做错了什么?
答案 0 :(得分:1)
这不对
在app \ View \ Territory中,我有一个文件spreadsheet.ctp:
您还需要将视图(而不仅仅是布局)放在以扩展名命名的子目录中:
应用\视图\领土\ CSV \ spreadsheet.ctp