Pregunta

I'm trying to implement dropzone.js into my CakePHP application. So far it all went fine. Except When I receive an error, it displays the whole HTML error page, not rendered. Which turns into a bunch of HTML code, not quite readable and because the error box becomes so big I cannot click the "remove" button. See picture below:

As soon as I receive an error: enter image description here

When I hover the box, after receiving an error: enter image description here

I know the reason is that dropzone.js recognizes the error because of the 500 header of the Ajax page (I throw an Exception if something goes wrong). And CakePHP renders a complete layout for a 500 error page. So it's not possible for me to just view a one row error. And I really need the 500 header, because else dropzone.js thinks everything went fine....

So my question: Is it possible to NOT render the 500 error layout, when getting a 500 error within a specific Controller method? I don't want to completely disable the 500 error layout rendering. Only for AJAX pages.

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'));
}
¿Fue útil?

Solución

  1. You can change retuned status code by use statusCode method of CakeResponse class. Something like this: $this->response->statusCode(404);
  2. It's a little not correct to use NotFoundException to return http status code. At least you can create your own application exception

Please, check Creating your own application exceptions

You will easily to define one exceptions:

class MissingWidgetException extends CakeException {};

And after that you can use it and send http status code which you need Creating custom status codes :

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

501 is http status code.

Hope, this will help to find out right solution.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top