2014-02-19 38 views
0

我試圖在我的CakePHP應用程序中實現dropzone.js。到目前爲止,這一切都很順利。除了當我收到一個錯誤,它會顯示整個HTML錯誤頁面,而不是渲染。這會變成一堆HTML代碼,不太可讀,因爲錯誤框變得如此之大,我無法點擊「刪除」按鈕。見下圖:CakePHP Dropzone.js錯誤Ajax

當我收到一個錯誤: enter image description here

當我徘徊箱子,接收到錯誤後:enter image description here

我知道原因是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')); 
} 
+0

簡答:是的,這是可能的。如果您提供更多的源代碼,我們可以提供更具體的建議。 – Vadim

+0

好的,我會在辦公室後立即發佈CakePHP方法,以便將dropzone.js上傳添加到數據庫(此函數也會在明天創建例外,並因此創建500個錯誤)。 –

+0

Voila,見上文。 –

回答

0
  1. 您可以通過使用更改重新調整狀態代碼CakeResponse類的statusCode方法。像這樣:$this->response->statusCode(404);
  2. 使用NotFoundException返回http狀態碼有點不正確。至少你可以創建自己的應用程序異常

請檢查Creating your own application exceptions

你會很容易定義一個例外:

class MissingWidgetException extends CakeException {}; 

之後,你可以使用它,併發送HTTP狀態碼您需要Creating custom status codes

throw new MissingWidgetHelperException('Its not here', 501); 

501是http狀態碼。

希望,這將有助於找出正確的解決方案。

+0

謝謝!到目前爲止,它沒有問題。除了我認識到我真的需要500錯誤,以防止dropzone.js完成上傳。它只停在500錯誤。那麼是否有可能爲當前的控制方法創建一個自定義500佈局?我可以在錯誤中更改500.ctp,但我想要其他控制器方法的佈局.... –

+1

500錯誤它只是服務器返回http狀態代碼500,因此,您可以使用'$ this-> response-> statusCode( 500);'和渲染,你真的需要。關於如何返回一些http狀態碼的更多細節可以在這裏找到:http://stackoverflow.com/questions/1100867/how-do-you-specify-an-http-status-code-in-cakephp/20757644 – Vadim

+0

好吧!它工作得很好,現在:)我沒有得到大量的代碼了:)謝謝你的幫助! –