![]() |
Somewhat confused about URL Helper - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28) +--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30) +--- Thread: Somewhat confused about URL Helper (/showthread.php?tid=87313) |
Somewhat confused about URL Helper - joho - 04-06-2023 I've only been playing with CI for a few days, so forgive me if this turns out to be a case of ignorance or misreading something. Regarding the URL Helper functions, why is the "CI master-file" (index.php) often included in the output? Wouldn't it be far more useful if I was actually getting URLs back that matched what the browser things/uses? I mean, URL functions are most often used to create an application that is self-contained, to allow for a dynamic configuration scenario (as opposed to hard coding value), no? My playground environment is https://mydomain.com/ltest The current section I'm playing with is https://mydomain.com/ltest/subsection The current route I'm playing with is https://mydomain.com/ltest/subsection/edit So... site_url() returns "https://mydomain.com/ltest/index.php" base_url() returns "https://mydomain.com/ltest" index_page returns "index.php" url_to('subsection::edit') returns "https://mydomain.com/ltest/index.php/subsection/edit" If I were to use any of these returned URLs in, for example, a form, I would still have to manually override things, or force things, which I don't want to. I want the application to be able to figure out what a correct and absolute URL is. I realize I can do manual replacing, remove "index.php" from the returned data, and so on. But I'm curious as to the reasoning behind the logic that is in place now. I guess I was thinking more along the lines of these: $_SERVER['REQUEST_URI'] = "/ltest/subsection/edit" $_SERVER['HTTP_HOST'] and $_SERVER['SERVER_NAME'] = "mydomain.com" To have something I could put in a <form method="post" action="..."> to get an absolute (and correct) URL. I do realize that most "apps" don't live in a sub-directory or "subsection", and that it may be an edge case, but I'd still like to make the app dynamically adjust for this possibility. Maybe I configured something wrong. (This is using PHP 8.1.2 with Nginx) I did find that by exposing 'request_uri' as a parameter from the controller to the view, .e.g. Code: $data['request_uri'] => $this->request->getUri() I can do this in the view, so I guess that works better for my purposes. Code: <form method="post" action="<?= $request_uri ?>"> That gives me the URL I want for the form action, which is https://mydomain.com/ltest/subsection/edit RE: Somewhat confused about URL Helper - joho - 04-06-2023 I'm guessing I'll soon be told to read this: https://codeigniter.com/user_guide/general/urls.html#urls-url-structure ![]() Which would be a fair point. So it probably comes down to me not having wrapped my head around all that yet. RE: Somewhat confused about URL Helper - kenjis - 04-06-2023 This following code is probably wrong, even if it works in your local PC. PHP Code: url_to('subsection::edit') Because the controller name should be "Subsection", and the file be "Subsection.php". On case sensitive filesystems, the code would not work. https://codeigniter.com/user_guide/helpers/url_helper.html#url_to RE: Somewhat confused about URL Helper - kenjis - 04-06-2023 (04-06-2023, 01:31 AM)joho Wrote: Regarding the URL Helper functions, why is the "CI master-file" (index.php) often included in the output? Probably you already know, but the answer is: by default, CI outputs index.php in the URLs, because it works without mod_rewrite or rewriting the URL. But in most cases, we do not use such URLs, and we use rewriting and set Config\App::$indexPage to empty string. RE: Somewhat confused about URL Helper - InsiteFX - 04-06-2023 In CodeIgniter url's are always Controller / Method url_to is used for named routes. PHP Code: <?php RE: Somewhat confused about URL Helper - digitalarts - 04-07-2023 (04-06-2023, 10:58 PM)InsiteFX Wrote: In CodeIgniter url's are always Controller / Method I think you mean url_to CAN be used for named routes. It can also be used without naming the route. So, in the example you provided Code: url_to("Galleries::showUserGallery", 15, 12) Code: http://example.com/users/15/gallery/12 https://codeigniter.com/user_guide/helpers/url_helper.html#url_to RE: Somewhat confused about URL Helper - InsiteFX - 04-07-2023 In the pass I found that sometimes it helps to use the base_url() method with route_to(). |