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: route_to() does not return relative part of the path #2971

Closed
SoulReaver opened this issue May 11, 2020 · 7 comments
Closed

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

SoulReaver opened this issue May 11, 2020 · 7 comments
Labels
bug Verified issues on the current code behavior or pull requests that will fix them

Comments

@SoulReaver
Copy link

SoulReaver commented May 11, 2020

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

  • OS: Windows 10
  • Web server: Apache 2.4.41
  • PHP version: 7.3.12
@SoulReaver SoulReaver added the bug Verified issues on the current code behavior or pull requests that will fix them label May 11, 2020
@jreklund
Copy link
Contributor

jreklund commented May 11, 2020

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
Copy link
Author

SoulReaver commented May 12, 2020

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
Copy link
Contributor

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
Copy link
Member

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
Copy link
Member

kenjis commented Jul 5, 2022

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

@SoulReaver
Copy link
Author

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
Copy link
Member

kenjis commented May 23, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Verified issues on the current code behavior or pull requests that will fix them
Projects
None yet
Development

No branches or pull requests

4 participants