Welcome Guest, Not a member yet? Register   Sign In
trailing slash problem [Solved]
#2

[eluser]fchristant[/eluser]
Ok, I'll hereby answer my own question. I managed to get it to work using the method below:

Code:
function initbasepath() {
        
// this function calculates how deep the current URI is compared to the base URI
// and returns a path from the current URI towards the base path
// the result can be used to create relative links that work no matter how
// deep the current URI is.
        
// the current URL
$uri = $this->uri->uri_string();

// check if the current URL ends with a slash
$trailingslash = substr($uri,strlen($uri)-1,1)== '/' ? true : false;
// count the number of segments in the URL, depending on the trailing slash
$segments = ($trailingslash) ? $this->uri->total_segments() + 1 : this->uri->total_segments() ;
// calculate path towards base URL based on #segments
$path = '';
if ($segments==1) {
   $path='./';
} else {
   for ($i=0;$i<$segments-1;$i++) {
      $path .= '../';
   }
}
        
// set the basepath parameter, this can be used on all views to prepend links
$this->basepath = $path;
}

This method is part of my base controller, which extends all my controllers. The function is quite useful. I can simply prepend $this->basepath to all links in my views, it will always have the correct relative path to the base url, no matter how deep the nesting of the current URL and no matter the trailing slash or not.

There is obviously nothing wrogn with using CI's base_url() either, it's just that I wanted to use relative links, not absolute ones. This also saves quite a lot of HTML chars and thus bandwidth.


Messages In This Thread
trailing slash problem [Solved] - by El Forum - 01-17-2009, 11:01 AM
trailing slash problem [Solved] - by El Forum - 01-18-2009, 04:47 AM
trailing slash problem [Solved] - by El Forum - 01-18-2009, 05:11 AM
trailing slash problem [Solved] - by El Forum - 01-18-2009, 06:17 AM
trailing slash problem [Solved] - by El Forum - 01-18-2009, 08:55 AM
trailing slash problem [Solved] - by El Forum - 01-18-2009, 11:20 PM
trailing slash problem [Solved] - by El Forum - 01-19-2009, 01:43 AM
trailing slash problem [Solved] - by El Forum - 01-19-2009, 01:50 AM
trailing slash problem [Solved] - by El Forum - 01-19-2009, 02:09 AM



Theme © iAndrew 2016 - Forum software by © MyBB