Welcome Guest, Not a member yet? Register   Sign In
ruri_string() when on index page

Here's the function (in v1.5.4):
function ruri_string()
    return '/'.implode('/', $this->rsegment_array()).'/';

You can see it sandwiches the implosion of rsegment_array() between two slashes. The problem is that, when on the index page of the site, the segment array is actually empty, so the returned value is "//". Not exactly the result we're looking for.

The fault doesn't actually lie with this function, though, but this illustrates the problem nicely. The real issue is that the rewritten segment array *should* always contain the controller and method segments at the very minimum, even on the index page, but this currently isn't the case. This issue was reported months ago. Is this fixed in the SVN?

This issue doesn't present a problem for most sites, but it does for mine. My CMS is linking content against the rewritten url, which needs to be correct for all pages.


Further to this issue:

There is a logic error in Router::_compile_segments() that causes this issue. This method terminates early if there are no segments in the actual uri used to access the current page. The early termination prevents the rewritten segment array from being updated, which happens at the end of the function:

// Update our "routed" segment array to contain the segments.
// Note: If there is no custom routing, this array will be
// identical to $this->segments
$this->rsegments = $segments;

The included comment is technically incorrect since it doesn't take the index page into consideration. The rewritten segment array should always contain at least 2 segments. It should always represent the real "path" to the controller method requested, including the default controller's index method.

[eluser]Derek Allard[/eluser]
Hey Ted. Sorry sir. When you say this was reported... was a bug_tracker entry made, or just on the forums? I couldn't find an entry in bug track. That's ok now, and you can make one if you want, but I'm aware of this now, so if you could just give me the previous report and any other information you have, I'll address it now. If you have a suggested resolution, I'd be really happy to take a look.

Working on a Saturday morning, Aren't you dedicated, Derek! (wait a minute... look at me) :-)

I don't know if this was officially reported, but it was discussed. The problem seems to originate back in the Router class. Since I have already subclassed that, I simply added the following workaround for this issue:

function _set_route_mapping() {
    // if we're on the index page, fix the segments array to include the controller and method
    if (count($this->rsegments) < 2) {
        $this->rsegments = array($this->class, $this->method);

It calls the parent method, and then checks to see if rsegments has the right number of segments. If not, it resets them.

The flow is:

1. Router::_set_route_mapping()
2. Router::_parse_routes()
3. Router::_compile_segments()

The problem is that _set_route_mapping() shortcuts if there is no URI to process, and as a result _compile_segments() is never called, but that is where rsegments actually gets updated.

Maybe that will give you some ideas for a solution.


This actually brings up another issue... the default route is still a valid route, but based on the above flow, it's currently not possible to override the default route in the config/routes.php file. This is probably never done, but since you're working on the issue, it might be something to address anyway.

$routes['default_controller'] = 'welcome';
That sets the default route to 'welcome/index';

However, we may want to direct requests for that route to something else, so we might do this:
$routes['welcome/index'] = 'alternate/route';

Since _set_route_mapping() shortcuts if it doesn't see a URI, then this custom route would never be processed. It would only be processed if http://site.com/welcome/index was used, which is inconsistent.

So, I think the solution, if there is no URI, is to manually compose the URI, and use that in the rest of the processing. Don't just shortcut the flow.

Does that seem to make sense to you?

[eluser]Derek Allard[/eluser]
OK, its 6 days since you wrote this and I haven't been able to hit it. Let's go with a formal bug report now (link to this thread) so that the whole dev team might hit it, and not just me. Wink

[quote author="Derek Allard" date="1200772496"]OK, its 6 days since you wrote this and I haven't been able to hit it. Let's go with a formal bug report now (link to this thread) so that the whole dev team might hit it, and not just me. Wink[/quote]

You haven't been able to fix the ruri_string() issue?

As explained above, the Router shortcuts the process (in the _set_route_mapping() method) when there is no route, so the _compile_segments() method never gets a chance to set the rsegments variable. I solved the problem (in a subclass) by setting the rsegments variable manually if it's still empty after _set_route_mapping() executes, but this could be integrated directly into that method.

Do you need a code example?

Theme © iAndrew 2016 - Forum software by © MyBB