Welcome Guest, Not a member yet? Register   Sign In
Somewhat confused about URL Helper
#1
Question 
(This post was last modified: 04-06-2023, 01:59 AM by joho.)

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 ?>">
..
</form>

That gives me the URL I want for the form action, which is https://mydomain.com/ltest/subsection/edit
Reply
#2
Big Grin 

I'm guessing I'll soon be told to read this: https://codeigniter.com/user_guide/gener...-structure

Cool
 
Which would be a fair point. So it probably comes down to me not having wrapped my head around all that yet.

-joho
Reply
#3

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/helpe...tml#url_to
Reply
#4

(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.
Reply
#5

In CodeIgniter url's are always Controller / Method

url_to is used for named routes.

PHP Code:
<?php

// The route is defined as:
$routes->get('users/(:num)/gallery/(:num)''Galleries::showUserGallery/$1/$2', ['as' => 'user_gallery']); // the as => is the named route

?>

<!-- Generate the URI to link to user ID 15, gallery 12: -->
<a href="<?= url_to('user_gallery'1512?>">View Gallery</a>  <!-- user_gallery is the named route. -->
<!-- Result: 'http://example.com//users/15/gallery/12' --> 
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#6

(04-06-2023, 10:58 PM)InsiteFX Wrote: In CodeIgniter url's are always Controller / Method

url_to is used for named routes.

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)
would also result in
Code:
http://example.com/users/15/gallery/12
unless another route with a higher priority also pointed to the showUserGallery method of the Galleries controller.

https://codeigniter.com/user_guide/helpe...tml#url_to
Reply
#7

In the pass I found that sometimes it helps to use the base_url() method with route_to().
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB