Welcome Guest, Not a member yet? Register   Sign In
Redirect() ignore baseURL?
#1

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/i...ler/save/1

...after the data is updated in the database, I want to redirect to:
http://localhost:8888/ci4-test1/public/i...bum/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?
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
Reply
#2

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’ ) );
Reply
#3

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...l#redirect
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
Reply
#4

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

redirect()->to('/foo') means redirect to the url /foo.
Simpler is always better
Reply
#5

(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/CodeIgnit...med-routes
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
Reply




Theme © iAndrew 2016 - Forum software by © MyBB