Welcome Guest, Not a member yet? Register   Sign In
Case insensitivity for controller URI segments
#1

[eluser]Phil Sturgeon[/eluser]
At work I needed a way to allow controller names to be called up insensitive of case. I found a way to do it but it seems a little over the top.

Either add this one-liner to systems/libraries/Router.php at the start of _validate_request()...

Code:
$segments[0] = strtolower($segments[0]);

... or create application/libraries/MY_Router.php and put the code below in the new file.

Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* CodeIgniter
*
* An open source application development framework for PHP 4.3.2 or newer
*
* @package        CodeIgniter
* @author        ExpressionEngine Dev Team
* @copyright    Copyright (c) 2008, EllisLab, Inc.
* @license        http://ellislab.com/codeigniter/user-guide/license.html
* @link        http://codeigniter.com
* @since        Version 1.0
* @filesource
*/

// ------------------------------------------------------------------------

/**
* Router Class
*
* Parses URIs and determines routing
*
* @package        CodeIgniter
* @subpackage    Libraries
* @author        ExpressionEngine Dev Team
* @category    Libraries
* @link        http://ellislab.com/codeigniter/user-guide/general/routing.html
*/
class MY_Router extends CI_Router {

    /**
     * Validates the supplied segments.  Attempts to determine the path to
     * the controller.
     *
     * @access    private
     * @param    array
     * @return    array
     */    
    function _validate_request($segments)
    {
        $segments[0] = strtolower($segments[0]);
        // Does the requested controller exist in the root folder?
        if (file_exists(APPPATH.'controllers/'.$segments[0].EXT))
        {
            return $segments;
        }

        // Is the controller in a sub-folder?
        if (is_dir(APPPATH.'controllers/'.$segments[0]))
        {        
            // Set the directory and remove it from the segment array
            $this->set_directory($segments[0]);
            $segments = array_slice($segments, 1);
            
            if (count($segments) > 0)
            {
                // Does the requested controller exist in the sub-folder?
                if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$segments[0].EXT))
                {
                    show_404($this->fetch_directory().$segments[0]);
                }
            }
            else
            {
                $this->set_class($this->default_controller);
                $this->set_method('index');
            
                // Does the default controller exist in the sub-folder?
                if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$this->default_controller.EXT))
                {
                    $this->directory = '';
                    return array();
                }
            
            }

            return $segments;
        }

        // Can't find the requested controller...
        show_404($segments[0]);
    }
    

}
// END Router Class

/* End of file Router.php */
/* Location: ./system/libraries/Router.php */

I believe a config option for this should be added, as while it may not be handy for everyone there are certain circumstances in which this could be useful.
#2

[eluser]garymardell[/eluser]
Erm, mine are already case insensitive.
#3

[eluser]Phil Sturgeon[/eluser]
Mine would 404 if i tried to access http://example.com/controllername via http://example.com/ControllerName
#4

[eluser]TheFuzzy0ne[/eluser]
That's one of the main the differences between Winblows and Linux file systems.
#5

[eluser]Phil Sturgeon[/eluser]
This is on a virtualised Solaris installation running over a corporate network with Samba. I wonder which filesystem wins out.
#6

[eluser]xwero[/eluser]
another one liner hack would be in the index.php
Code:
$_SERVER['PATH_INFO'] = strtolower($_SERVER['PATH_INFO']);
If you don't need the extend the router library for other things you don't need to load the extended file.
#7

[eluser]TheFuzzy0ne[/eluser]
But that's so simple it's borderline insanity. Tongue I like it.
#8

[eluser]Phil Sturgeon[/eluser]
That is simple, and it is also insane... not in the good way. That will lower-case EVERYTHING, including any data sent to the controller or any identifiers used to retrieve the data. It will also break any methods in controllers that use upper case!

I will stick with my solution, it does the job. :-)
#9

[eluser]TheFuzzy0ne[/eluser]
The only issue is if you require anything within your URL to be case sensitive. Since PHP is not case sensitive by nature, it shouldn't affect the calling of any methods.
#10

[eluser]xwero[/eluser]
If you only want the first segment
Code:
list(,$first) = explode('/',$_SERVER['PATH_INFO']);
$_SERVER['PATH_INFO'] = preg_replace('/\/'.$first.'/','/'.strtolower($first),$_SERVER['PATH_INFO']);




Theme © iAndrew 2016 - Forum software by © MyBB