2

I have the following code in my controller:

class ContactsController extends AppController {
    public $helpers = array('Html', 'Form', 'Session');
    public $components = array('Session');

    public function index() {
        $this->set('contacts', $this->Contact->find('all'));
    }

    public function view($id) {
        if (!$id) {
            throw new NotFoundException(__('Invalid contact'));
        }

        $contact = $this->Contact->findById($id);
        if (!$contact) {
            throw new NotFoundException(__('Invalid contact'));
        }
        $this->set('contact', $contact);
    }

    public function add() {                 
        if ($this->request->is('post')) {
            $this->Contact->create();
            if ($this->Contact->save($this->request->data)) {
                $this->Session->setFlash('Your contact has been saved.');
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash('Unable to add your contact.');
            }
        }
    }
}

In the add() method I have the line $this->redirect(array('action' => 'index')); I'm expecting this line to redirect back to my index page within my view. But all I get is a blank white page.

Any help appreciated.

Regards, Stephen

Stephen
  • 4,715
  • 8
  • 53
  • 78

2 Answers2

18

Your code looks valid; I don't see an error in the code you provided.

There are a few possible causes;

  • there is an error somewhere else in your code, but cake php doesn't output the error message because debugging is disabled. Enable debugging by setting debugging to 1 or 2 inside app/Config/core.php - Configure::write('debug', 2);
  • you application is outputting something to the browser before the redirect header was sent. This may be caused by (non visible) white-space before or after any <?php or ?> . This will cause a 'headers already sent' warning, and the browser will not redirect. There are many questions regarding this situation here on StackOverflow, for example: How to fix "Headers already sent" error in PHP

Note; in CakePHP its good practice to put a return before any redirect statement; this will allow better Unit-testing of your application, i.e.:

return $this->redirect(array('action' => 'index'));

update; additional 'pointers' on locating the cause

Tracking these kind of problems may be troublesome, I'll add some pointers

  • If you're using the Auth or Security component, it's possible that one of these cause the 'problem' (e.g. authorization failed or the posted data is marked 'invalid', which will be handled by a 'blackHole()' callback. Disable both in your AppController to check if the problem is still present
  • If the problem is still present, it is possible that headers are being sent (as mentioned before), but no warning/error is presented. To find if and where those headers are sent, add this code to your 'add' action;

Debugging code:

if (headers_sent($file, $line)) {
    exit("headers were already sent in file: {$file}, line number: {$line}");
}
Community
  • 1
  • 1
thaJeztah
  • 27,738
  • 9
  • 73
  • 92
7

After lots of research finally I got a solution for that.

Please use

ob_start(); in AppController.php

ob_start();
class AppController extends Controller {
     function beforeFilter() {
         parent::beforeFilter();
     }
}
Chinmay235
  • 3,236
  • 8
  • 62
  • 93
  • why did this work!? i was having trouble for hours yet this simple trick solved the problem. i need some enlightenment!!! – Christian Burgos Jul 09 '17 at 07:52
  • Solved for me too, saved hours! Cheers bud. An explanation would be that AppController closes the buffer before the redirect code so we need to keep it open until it finishes processing – Muneeb Zulfiqar Sep 21 '22 at 04:14