CodeIgniter Forums
Redirect() ignore baseURL? - 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: Redirect() ignore baseURL? (/thread-73422.html)



Redirect() ignore baseURL? - includebeer - 04-22-2019

I have some troubles with the redirect function.

I test my code with MAMP on my Mac. I don't want to set up multiple subdomains so I have multiple CI4 installations in subdirectories like this:
http://localhost:8888/ci4-test1/public/index.php
http://localhost:8888/ci4-test2/public/index.php
http://localhost:8888/ci4-test3/public/index.php

I submit a form to this URL:
http://localhost:8888/ci4-test1/public/index.php/some_controller/save/1

...after the data is updated in the database, I want to redirect to:
http://localhost:8888/ci4-test1/public/index.php/album/edit/1

But here is my problem. It redirect to this URL and gives a 404 not found error:
http://localhost:8888/some_controller/edit/1

In .env I set the baseURL like this:
app.baseURL = 'http://localhost:8888/ci4-test1/public/'

In my controller I do the redirect like this:
return redirect()->to('/some_controller/edit/' . $id');

What am I doing wrong? Why is the directory name 'ci4-test1/public' not in the destination URL?


RE: Redirect() ignore baseURL? - MGatner - 04-22-2019

to() takes a prepared URL. If you want it to use the full URL pass site_url() in:
return redirect()->to( site_url( ‘your path’ ) );


RE: Redirect() ignore baseURL? - includebeer - 04-22-2019

Ok, thanks! That's a weird way of handling the redirections. I assumed it would redirect to the site url by default.
And what's the difference between redirect('foo/bar') and redirect()->to('foo/bar')?
The documentation is not clear on that point: https://codeigniter4.github.io/userguide/general/common_functions.html#redirect


RE: Redirect() ignore baseURL? - donpwinston - 04-22-2019

I believe redirect('foo') means redirect to the "named" route foo.

redirect()->to('/foo') means redirect to the url /foo.


RE: Redirect() ignore baseURL? - includebeer - 04-23-2019

(04-22-2019, 05:02 PM)donpwinston Wrote: I believe redirect('foo') means redirect to the "named" route foo.

redirect()->to('/foo') means redirect to the url /foo.

You are right! I found more info on named routes: https://codeigniter4.github.io/CodeIgniter4/incoming/routing.html#using-named-routes