我試圖在我的CakePHP應用程序中實現dropzone.js。到目前爲止,這一切都很順利。除了當我收到一個錯誤,它會顯示整個HTML錯誤頁面,而不是渲染。這會變成一堆HTML代碼,不太可讀,因爲錯誤框變得如此之大,我無法點擊「刪除」按鈕。見下圖:CakePHP Dropzone.js錯誤Ajax
當我收到一個錯誤:
當我徘徊箱子,接收到錯誤後:
我知道原因是dropzone.js承認錯誤由於Ajax頁面的500頭(如果出現問題,我會拋出異常)。 CakePHP爲500個錯誤頁面提供完整的佈局。所以我無法查看單行錯誤。我真的需要500頭,因爲否則dropzone.js認爲一切順利......
所以我的問題:是否有可能不呈現500錯誤佈局,當在特定的控制器方法中獲得500錯誤?我不想完全禁用500錯誤佈局呈現。僅適用於AJAX頁面。
public function admin_add($slug = null) {
if(!$slug || !$client = $this->Video->Client->find('first', array('conditions' => array('slug' => $slug)))) {
throw new NotFoundException(__('Invalid client'));
}
if ($this->request->is('post')) {
// If request contains files, continue
if (!empty($_FILES)) {
// Get slug from URL
$slug = substr($this->referer(), strrpos($this->referer(), '/')+1);
// Create new folder for the movies if it doesn't exist already
if (!file_exists(WWW_ROOT.'/files/'.$slug)) {
mkdir(WWW_ROOT.'/files/'.$slug, 0777, true);
}
$tempFile = $_FILES['file']['tmp_name'];
$targetPath = '/files/'.$slug.'/';
$targetFile = $targetPath. $_FILES['file']['name'];
// Create variable filename without the extension
$fileWithoutExt = preg_replace("/\\.[^.\\s]{3,4}$/", "", $_FILES['file']['name']);
// Add file to Video array
$video['Video'] = array('video' => $targetFile, 'screenshot' => '/files/'.$slug.'/screenshots/'.$fileWithoutExt.'.jpg', 'client_id' => $client['Client']['id']);
// unset($video);
// Try moving the file to their final directory
if(!move_uploaded_file($tempFile, WWW_ROOT.$targetFile)) {
throw new NotFoundException(__('Move image to "'.WWW_ROOT.$targetPath.'" failed'));
}
// Create new folder for the screenshots if it doesn't exist already
if (!file_exists(WWW_ROOT.'/files/'.$slug.'/screenshots/')) {
mkdir(WWW_ROOT.'/files/'.$slug.'/screenshots/', 0777, true);
}
// Try saving video to Video table in the database
if(!$this->Video->save($video)){
throw new NotFoundException(__('Failed connecting client with "'.$targetFile.'" in the database'));
}
}
$this->Session->setFlash(__('Videos successfully uploaded'), 'default', array(), 'success');
$this->redirect($this->referer());
}
$title_for_layout = $client['Client']['name'];
$this->set(compact('title_for_layout', 'client'));
}
簡答:是的,這是可能的。如果您提供更多的源代碼,我們可以提供更具體的建議。 – Vadim
好的,我會在辦公室後立即發佈CakePHP方法,以便將dropzone.js上傳添加到數據庫(此函數也會在明天創建例外,並因此創建500個錯誤)。 –
Voila,見上文。 –