How to redirect same page after form is saved in cakephp 3?

Hi,

$this->Form->create(${$viewVar}, ['role' => 'form', 'data-dirty-check' => true]
-------------
-------------
--------------
$this->Form->end()

I can submit form data. But it redirect to home page. But i want to be same page after submit.

1 Like

$this->redirect($this->referer());

or

$this->redirect(['controller' => 'controller', 'action' => 'action']);

https://book.cakephp.org/3.0/en/controllers.html#redirecting-to-other-pages

thanks for your reply

The referer normally works, but note that is not ‘safe’. $this->referer() is based on the PHP global $_SERVER['HTTP_REFERER'] and can have unexpected values or could even be changed by a third party (need confirmation, I just remember reading this somewhere).

I store the url of where I am at in session then can make link or a redirect from the session, I think 99 percent is done that way. Fairly standard.

If you’re just trying to reload the same page, e.g. object/edit page after save, just remove the $this->redirect() entirely from the edit() function in the Controller. Then it won’t redirect anywhere, but simply reload the same page.

public function edit($id = null)
{
    ...
    $this->Flash->success(__('The object has been saved.'));
    //return $this->redirect(['action' => 'index']);
    ...
}