CodeIgniter Forums
How can I redirect in Controller? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forum-28.html)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forum-30.html)
+--- Thread: How can I redirect in Controller? (/thread-69869.html)



How can I redirect in Controller? - iceleo - 01-25-2018

Hi,
I have a trouble, when I want to redirect to somewhere, my site is shutdowned. This is my code:

PHP Code:
return redirect('somewhere'); 

Let me know if I'm wrong.
Thanks!


RE: How can I redirect in Controller? - jreklund - 01-25-2018

Skip 'return' and you are good to go.
https://www.codeigniter.com/user_guide/helpers/url_helper.html?highlight=redirect#redirect


RE: How can I redirect in Controller? - iceleo - 01-25-2018

Hi Jreklund,

I'm sure you give me a document of CI version < version 4. This is CI4, this is the document of CI4 https://bcit-ci.github.io/CodeIgniter4/. Maybe, I need a help from the constructor, this is a bug, I think.

Thankyou!


RE: How can I redirect in Controller? - kilishan - 01-25-2018

The way you're using it used to work, but it was reworked a bit ago and that usage might have broken. See the examples in the docs but the quick version is:

Code:
return redirect()->to('somewhere');



RE: How can I redirect in Controller? - iceleo - 01-25-2018

Hi,

I have test in other hosting, it works with two method redirect('somewhere') and redirect()->to('somewhere'). Maybe, I have problem with this hosting. I'm sorry about that. 

But I have another problem, the redirect always redirect to base URL. Example:

The base URL: https://mydomain.com/path/
I want redirect https://mydomain.com/path/admin to https://mydomain.com/path/admin/login if not logged in.
My redirect code is
PHP Code:
return redirect()->to('admin/login'); 
And the redirect is: https://mydomain.com/admin/login

I think I defined $baseURL correctly.

PHP Code:
public $baseURL 'https://mydomain.com/path/'

Thanks!


RE: How can I redirect in Controller? - jreklund - 01-25-2018

(01-25-2018, 06:25 AM)iceleo Wrote: I'm sure you give me a document of CI version < version 4. This is CI4, this is the document of CI4 https://bcit-ci.github.io/CodeIgniter4/. Maybe, I need a help from the constructor, this is a bug, I think.
Didn't look at the sub forum on this one.
Haven't started using CI4 yet, so can't be of any help here. Sorry for popping by. :-)


RE: How can I redirect in Controller? - RobT - 10-30-2019

I have the same problem on CI v4.0.0rc3.

My .env file is configured as follow.
Code:
app.baseURL = 'http://localhost/path/'

My controller calls the following function.
PHP Code:
return redirect()->to('catalog'); 

I'm redirected to http://localhost/catalog instead of http://localhost/path/catalog.

How do I fix it?
Thanks.