简体   繁体   中英

Cake php redirect error [Can not modify header information]

I'm new to CakePHP. I'm just testing how it works. I use other frameworks in PHP. Here in CakePHP 3.6 freshly installed. I'm following CakePHP's blog tutorial. My code is:

 $article = $this->Articles->newEntity();
            if ($this->request->is('post')) {
                // Prior to 3.4.0 $this->request->data() was used.
                $article = $this->Articles->patchEntity($article, $this->request->getData());
                if ($this->Articles->save($article)) {
                    $this->Flash->success(__('Your article has been saved.'));
                   return $this->redirect(['action' => 'index']);
                }
                else {
                    $this->Flash->error(__('Unable to add your article.'));
                }

            }
            $this->set('article', $article);

The problem is while redirecting to another action. When I comment line return $this->redirect(['action' => 'index']); it works else it throws following error.

Warning (512): Unable to emit headers. Headers sent in file=C:\\Users\\Dipti\\blog\\vendor\\cakephp\\cakephp\\src\\Error\\Debugger.php line=853 [CORE\\src\\Http\\ResponseEmitter.php, line 48] Warning (2): Cannot modify header information - headers already sent by (output started at C:\\Users\\Dipti\\blog\\vendor\\cakephp\\cakephp\\src\\Error\\Debugger.php:853) [CORE\\src\\Http\\ResponseEmitter.php, line 148]

In your appController change

$this->loadComponent('RequestHandler');

to

$this->loadComponent('RequestHandler', [
    'enableBeforeRedirect' => false
]);

(I know, this is not a great answer. If I have time I will expand it)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM