• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
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/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?
Test your translation files with Translation Tester
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
Test your translation files with Translation Tester
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, 06: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
Test your translation files with Translation Tester
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.