Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: site_url() ignores Config\App::$baseURL when not passing $config #7290

Closed
c4user opened this issue Feb 21, 2023 · 11 comments
Closed

Bug: site_url() ignores Config\App::$baseURL when not passing $config #7290

c4user opened this issue Feb 21, 2023 · 11 comments
Labels
waiting for info Issues or pull requests that need further clarification from the author

Comments

@c4user
Copy link

c4user commented Feb 21, 2023

PHP Version

7.4

CodeIgniter4 Version

4.3.2

CodeIgniter4 Installation Method

Manual (zip or tar.gz)

Which operating systems have you tested for this bug?

Windows

Which server did you use?

apache

Database

MariaDB 10.2

What happened?

site_url() function ignores App->baseURL

Steps to Reproduce

Change config("App")->baseURL and check site_url("test") output.

// default baseURL = "http://localhost:8080/";
$app = config("App");
$app->baseURL = "https://test.com/";

return site_url("/login"); // output: http://localhost:8080/login

Expected Output

Output containing the new baseURL (https://test.com/login).

Anything else?

No response

@c4user c4user added the bug Verified issues on the current code behavior or pull requests that will fix them label Feb 21, 2023
@c4user
Copy link
Author

c4user commented Feb 21, 2023

Another test:

// default baseURL = "http://localhost:8080/";
$app = config("App");
$app->baseURL = "https://xxx.test.com/";
$app->allowedHostnames = [
   "xxx.test.com",
];

return site_url("/login"); // output: http://localhost:8080/login

@kenjis kenjis removed the bug Verified issues on the current code behavior or pull requests that will fix them label Feb 22, 2023
@kenjis kenjis changed the title Bug: site_url Bug: site_url() ignores Config\App::$baseURL when not passing $config Feb 22, 2023
@kenjis
Copy link
Member

kenjis commented Feb 22, 2023

Thank you for reporting!

But unfortunately, this is not a bug, or we probably cannot fix it.
Because since v4.3.0, Multiple Domain Support is added.
https://codeigniter4.github.io/CodeIgniter4/changelogs/v4.3.0.html#multiple-domain-support

The current URL is determined when a Request object is created.
And you cannot change it later. Because the current URL is never changed
in a request.

If you want to change the baseURL, please pass the Config object to site_url().

        $app = config("App");
        $app->baseURL = "https://test.com/";

        return site_url("/login", null, $app);

Why do you need to change the baseURL? Can you explain your use case?

@c4user
Copy link
Author

c4user commented Feb 22, 2023

Actually my problem was related to form_open. When I examined form_open function, found site_url. The form_open/site_url functions related to the baseurl cause problems when using dynamic subdomains.


$app = config("App");
$app->baseURL = "https://xxx.test.com/";
$app->allowedHostnames = [
   "xxx.test.com",
];

I actually do this definition with filters/pre. At least if it is possible to change it with the filter pre, there will be no problem.

@kenjis
Copy link
Member

kenjis commented Feb 22, 2023

I think if you set $allowedHostnames in Config\App, you don't need it in the filter.

@c4user
Copy link
Author

c4user commented Feb 22, 2023

How can I use code for domain names such as userX.test.com, random-chars.test.com, can you show an example? Subdomains are set by the user.

I think changes need to be made with filters/pre.

It also doesn't support regex even. At least that could have been.

I cannot upgrade version for this reason.

@kenjis
Copy link
Member

kenjis commented Feb 22, 2023

How do you know the domain names now?

@c4user
Copy link
Author

c4user commented Feb 22, 2023

I set baseURL in Filters/pre or controller. So I can use form_open, site_url etc. without problem.

@c4user
Copy link
Author

c4user commented Feb 22, 2023

I get it from session or database according to user's choice. Then I edit it with baseUrl. For example, if user chooses kenjis for subdomain, I read from session and edit baseURL to kenjis.test.com.

@kenjis
Copy link
Member

kenjis commented Feb 23, 2023

Sorry, now the code round the current URI is very complicated.

Please try:

<?php

namespace App\Controllers;

class Home extends BaseController
{
    public function index()
    {
        $uri = $this->request->getUri();

        // Update the current URI object
        $uri->setBaseURL('https://test.com/');
        $uri->setScheme('https');
        $uri->setHost('test.com');

        helper('form');

        return form_open("login");
    }
}

@kenjis kenjis added the waiting for info Issues or pull requests that need further clarification from the author label Feb 27, 2023
@c4user
Copy link
Author

c4user commented Feb 27, 2023

thank you!

@c4user c4user closed this as completed Feb 27, 2023
@kenjis
Copy link
Member

kenjis commented Feb 27, 2023

Since the URI-related code is complicated, difficult to understand, and a bit buggy,
I have sent PR #7282 with the code reorganized and rewritten.

However, since it brings breaking changes, I do not yet know when it will be merged or not.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
waiting for info Issues or pull requests that need further clarification from the author
Projects
None yet
Development

No branches or pull requests

2 participants