CodeIgniter Forums
redirecting from another method - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: redirecting from another method (/showthread.php?tid=82730)



redirecting from another method - vgrigorov - 08-16-2022

butHello, all,
I have the following question. In my controller (Service) I easily redirect with:

PHP Code:
return redirect()->to('http://' $_SERVER['HTTP_HOST'] . $this->data['router']['edit'] . $Id '?add=1'); 


But when I call some method from my controller and this method is defined in the BaseController for example, the redirection does not work.
Here is a fragment from my code, In my controller:
PHP Code:
class Service extends BaseController
{
    public function edit()
    {
        $this->editData();
    }


And here is the editData() method in the BaseController

PHP Code:
protected function editData($Id)
    {

        if ($this->request->getMethod() === 'post') {
            if ($Id) {
                $res $this->saveData($Id);
                $this->data['res'] = ['type' => 1'mess' => ($res) ? lang($this->data['lang'] . '.mess.ok.edit') : lang($this->data['lang'] . '.mess.err.edit')];
            } else {
                $Id $this->saveData();
                if ($Id) {
                    return redirect()->to('http://' $_SERVER['HTTP_HOST'] . $this->data['router']['edit'] . $Id '?add=1');
                } else {
                    $this->data['res'] = ['type' => 0'mess' => lang($this->data['lang'] . '.mess.err.add')];
                }
            }
        }
    

If I change it to "return editData()" in my edit() method it works, but this does not works for me. So is there a way to redirect directly from the editData method?


RE: redirecting from another method - iRedds - 08-16-2022

For a redirect to work, your controller must return a RedirectResponse, and it doesn't return anything.


RE: redirecting from another method - kenjis - 08-16-2022

Make sure to validate $_SERVER['HTTP_HOST'].