CodeIgniter4: Bug: current_url() returning wrong value when different host

PHP Version

8.0

CodeIgniter4 Version

4.1.9

CodeIgniter4 Installation Method

Composer (using codeigniter4/appstarter)

Which operating systems have you tested for this bug?

Windows

Which server did you use?

apache

Database

No response

What happened?

The function current_url() returns the $baseURL in app/Config/App.php instead of the real current url in the browser. This leads to confusion, know the documentation says:

You can restrict groups of routes to function only in certain domain or sub-domains of your application by passing the “hostname” option along with the desired domain to allow it on as part of the options array:

https://codeigniter.com/user_guide/incoming/routing.html?highlight=router#limit-to-hostname

Which means an app could have multiple domain pointing at it, but current_url() would only return the one set in config.

Use case: CMS, multi-tenant app, multi-vendor app, etc.

Steps to Reproduce

Call current_url() when loading the app with a host like sub.example.com while in the config, you would set $baseURL to www.example.com for example.

Expected Output

current_url() is the name implies should retour the current url and not the current url with $baseURL as host.

Anything else?

No response

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 26 (15 by maintainers)

Most upvoted comments

Hello @szajens , I am a beginner like you. I did not use a composer, and I had no knowledge of unit testing. I am learning recently. When I first used the composer, everything was vague to me. But the more I use it, the more I regret why I did not know it before. I suggest you use it. To learn the unit test, because I was confused recently, I suggest you follow the steps below in order.

  1. Read a short article about it here
  2. Watch the 10-part unit test video here
  3. For more mastery of the official documentation of the unit, see here.
  4. From now on, write a unit test at the same time for all the code you write. If you have a problem, I will be with you through forum, of course, as far as I know.

Thank you, I have just installed composer, I have exactly the same feeling as you wrote.

@szajens Please share once you have it done! I’m curious about your solution.

PECE Routing for CI4

I’m sorry it took so long, but I had a lot of other work.

I still have to put the library in order. I have a problem with English, and I will need help from someone who knows English well to correct the README.md

I need to work on the url_to helper in next version

Edit: When using the pece library, url_to should be return the defined options in routes, it must return a defined domain. Currently returns the current request.

Something similar like my libs could be done in CI5

@szajens Please share once you have it done! I’m curious about your solution.