codeigniter4 / CodeIgniter4

Open Source PHP Framework (originally from EllisLab)
https://codeigniter.com/
MIT License
5.4k stars 1.9k forks source link

Bug: route_to() does not return relative part of the path #2971

Closed SoulReaver closed 4 years ago

SoulReaver commented 4 years ago

Describe the bug route_to() helper function according to docs:

Generates a relative URI ...

While the generated path is not relative but "rooted" (I am not sure how it's called properly). If used in views, e.g. to build links/anchors, those will be relative to domain, not to base_url.

CodeIgniter 4 version 4.0.3 - current release

Affected module(s) Routes or user-guide/docs

Expected behavior, and steps to reproduce if appropriate Let's consider this example: example page in service: example.com/ci-blog/login base url: example.com/ci-blog/ url to the page relative to base url: login route_to returns: /login which points to: example.com/login

Expected behavior - route_to to return parts of path relative to base_url, i.e. without slash at the beginning.

Current workaround for CI user is to add dot at the beginning of every link generated by route_to or remove slash manually.

Context

jreklund commented 4 years ago

Maybe it's referenced wrong in the documentation or it should return an absolute url like base_url does.

But it should not remove / as it won't work for most people. Generally people don't develop applications in sub-folders.

If you are on this url and use route_to('login') with suggested fix. example.com/ci-blog/admin/

You will get: login example.com/ci-blog/admin/login

Instead of: /login example.com/login

(or with baseUrl) example.com/ci-blog/login example.com/ci-blog/login

SoulReaver commented 4 years ago

If you have an app in subfolder, you always must use base-tag to set the base link (forgot to mention that) OR use absolute links/paths. Either way, "proper" route_to would work for both cases, current, "accidentally" only when project runs directly rooted at domain.

As for "generally people don't develop applications in sub-folders" - if something generally is not a bug/issue, does not change the fact that it is one.

I am not saying that it HAS to be changed, but I see it as a bug (at least doc-bug) and am suggesting that change. In the end, that's decision for dev team to make. I can understand that route_to is heavily used everywhere as it's very nice helper function and that might (but not necessarily) break many projects if they decide to update. But hey, breaking changes happen and it's your decision as dev, whether you will update your tools or not. Also keep in mind that CI4 is not the current version of CI, but version in development (clearly written on main page: CodeIgniter 4 is the upcoming version of the framework), so if you're using it, you should be ready to face breaking changes in future versions.

jreklund commented 4 years ago

I didn't say it wasn't a bug, I'm saying that we should not remove /. As that will create more issues than the current behavior. We need to append baseUrl or leave it as it, but it will work for most people to append baseUrl.

If it weren't intended to work this way to begin with. Then it's a documentation problem.

michalsn commented 4 years ago

Removing / or adding baseUrl won't happen since it would be a BC change.

In most cases (when we don't host a website in a sub-folder) it's convenient to use route_to() as it is, but in other cases, I would recommend using this function together with site_url(), which will produce a nice URL.

kenjis commented 2 years ago

It is recommended to use url_to() now. url_to() equals site_url(route_to()).

SoulReaver commented 1 year ago

Yes, it would, but at the same time, I see no reason to stop using my fixed helper function that actually returns what route_to originally was meant to return ( prefixing original route_to with a dot is all that had to be done).

Fun thing is, that I came back here because those (url related, base_url among them) helper functions changed. I wasn't updating the "engine" for quite some time. I did so now, as I have to write some new stuff for my project, jumping from 4.0.x to 4.3.x. Suddenly addresses got messed up, because functions that had to remain unchanged, as that would be a breaking change, suddenly changed … not to mention, that functions' purpose changed as well.

kenjis commented 1 year ago

The following two lines are not the same.

<a href="/login">
<a href="./login">

So if we change the behavior of route_to(), apps that have code like <a href="<?= route_to('login') ?>"> will break. Such usage is kind of misuse, but the code seems likely.

The framework function behavior may be changed because of bug fixes. All changes are documented in the user guide: https://codeigniter4.github.io/CodeIgniter4/changelogs/index.html If it is not documented, report it as a bug in the documentation.