• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Nasty little bug - easy fix

#1
File: system/core/Config.php Line: 269

- $uri .= $suffix;
+ if (trim($uri) !== '') $uri .= $suffix;

If $config['url_suffix'] is set to a value like $config['url_suffix'] = '.html' the home link will be generated as
http://www.mydomain.com/.html

The fix obove is solving the problem, however I hate to change the code in core, forcing me to keep track and check on the next release.
Reply

#2
When having a suffix set in the config will not add the suffix when calling
PHP Code:
site_url(); // Result http://www.yourdomain.com 

It will add the suffix when passing an URI argument to this method
PHP Code:
site_url('some-kind-of-page'); // Result http://www.yourdomain.com/some-kind-of-page.html 

But when the argument is ?test=1234
PHP Code:
site_url('?test=1234'); // Result http://www.yourdomain.com/.html?test=1234 
This last one should be fixed. When the ? is on the first position the suffix should not be added to the URI

But to fix this, you don't need to edit the core. You can override the site_url method by adding it to a MY_url_helper file.

EDIT:
I have added a site_url method to a MY_url_helper. It sets the correct URL when only passing an query string
PHP Code:
/**
 * Site URL updated
 *
 * Returns base_url . index_page [. uri_string]
 *
 * @uses CI_Config::site_url()
 *
 * @param string|string[] $uri URI string or an array of segments
 * @param string $protocol
 * @return string
 */
function site_url($uri ''$protocol NULL) {
 
   // Get the CI super object
 
   $ci =& get_instance();
 
 
   // Get the set suffix
 
   $suffix $ci->config->config['url_suffix'];
 
 
   // If the first character of an URI is a ? then don't use the suffix
 
   if( ($offset strpos($uri'?')) !== FALSE && $offset === 0) {
 
       // Lets clear the config item for this call
 
       $ci->config->set_item('url_suffix''');
 
 
       // Get the site url from the original site_url method
 
       $site_url $ci->config->site_url($uri$protocol);
 
 
       // Reset the suffix
 
       $ci->config->set_item('url_suffix'$suffix);
 
 
       // Lets remove the / in front of the ?
 
       $base_url $ci->config->config['base_url'];
 
       $new_uri str_replace(base_url(), ''$site_url);
 
       ifstrlen($new_uri) > 0) {
 
           $site_url $base_url.$new_uri;
 
           return $site_url;
 
       }
 
   }
 
 
   return $ci->config->site_url($uri$protocol);

Reply

#3
(11-07-2015, 08:41 AM)Martin7483 Wrote: When having a suffix set in the config will not add the suffix when calling
PHP Code:
site_url(); // Result http://www.yourdomain.com 

It will add the suffix when passing an URI argument to this method
PHP Code:
site_url('some-kind-of-page'); // Result http://www.yourdomain.com/some-kind-of-page.html 

But when the argument is ?test=1234
PHP Code:
site_url('?test=1234'); // Result http://www.yourdomain.com/.html?test=1234 
This last one should be fixed. When the ? is on the first position the suffix should not be added to the URI

But to fix this, you don't need to edit the core. You can override the site_url method by adding it to a MY_url_helper file.

EDIT:
I have added a site_url method to a MY_url_helper. It sets the correct URL when only passing an query string
PHP Code:
/**
 * Site URL updated
 *
 * Returns base_url . index_page [. uri_string]
 *
 * @uses CI_Config::site_url()
 *
 * @param string|string[] $uri URI string or an array of segments
 * @param string $protocol
 * @return string
 */
function site_url($uri ''$protocol NULL) {
 
   // Get the CI super object
 
   $ci =& get_instance();
 
 
   // Get the set suffix
 
   $suffix $ci->config->config['url_suffix'];
 
 
   // If the first character of an URI is a ? then don't use the suffix
 
   if( ($offset strpos($uri'?')) !== FALSE && $offset === 0) {
 
       // Lets clear the config item for this call
 
       $ci->config->set_item('url_suffix''');
 
 
       // Get the site url from the original site_url method
 
       $site_url $ci->config->site_url($uri$protocol);
 
 
       // Reset the suffix
 
       $ci->config->set_item('url_suffix'$suffix);
 
 
       // Lets remove the / in front of the ?
 
       $base_url $ci->config->config['base_url'];
 
       $new_uri str_replace(base_url(), ''$site_url);
 
       ifstrlen($new_uri) > 0) {
 
           $site_url $base_url.$new_uri;
 
           return $site_url;
 
       }
 
   }
 
 
   return $ci->config->site_url($uri$protocol);


As a matter of fact
Code:
site_url(' '); // Result http://www.yourdomain.com/ .html
Reply

#4
Haha, did not test that scenario. But it's a quick fix.
Reply

#5
I have updated the site_url method of MY_url_helper

PHP Code:
    /**
     * Site URL updated to set a correct URL when passing only white space
     * or passing an query string starting with a "?"
     *
     * Returns base_url . index_page [. uri_string]
     *
     * @uses CI_Config::site_url()
     *
     * @param string|string[] $uri URI string or an array of segments
     * @param string $protocol
     * @return string
     */
 
   function site_url($uri ''$protocol NULL) {
 
       // Get the CI super object
 
       $ci =& get_instance();
 
 
       // Get the set suffix
 
       $suffix $ci->config->config['url_suffix'];
 
 
       // Check if uri is an array
 
       ifis_array($uri) ) {
 
           $uri implode('/'$uri);
 
       }

 
       // Trim all white space
 
       $uri trim($uri);
 
 
       // Is a "?" present and the first character?
 
       if( ($offset strpos($uri'?')) !== FALSE && $offset === 0) {
 
           // Lets clear the config item for this call
 
           $ci->config->set_item('url_suffix''');
 
 
           // Get the site url from the original site_url method
 
           $site_url $ci->config->site_url($uri$protocol);
 
 
           // Reset the suffix
 
           $ci->config->set_item('url_suffix'$suffix);
 
 
           // Lets remove the "/" in front of the ?
 
           $base_url $ci->config->config['base_url'];
 
           $new_uri str_replace(base_url(), ''$site_url);
 
           ifstrlen($new_uri) > 0) {
 
               $site_url $base_url.$new_uri;
 
               return $site_url;
 
           }
 
           // We should never reach this line
 
           return $site_url;
 
       }
 
 
       return $ci->config->site_url($uri$protocol);

Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.