Welcome Guest, Not a member yet? Register   Sign In
Automagically convert hyphens into underscores for class/method calls in URI
#1

[eluser]derrin[/eluser]
I was really getting sick of having to create route exceptions for every controller that I wanted to separate words with hyphens in the URI(since class and method names only allow underscores...).

The only part I updated was the _validate_request method in the Router class and added the _strip_hyphens method. Pretty simple.

See code below.

Code:
<?
    class MY_Router extends CI_Router {
        
        function MY_Router() {
            parent::CI_Router();
        }
        
        function _validate_request($segments) {
            
            $segments[0]    = $this->_strip_hyphens(@$segments[0]);
            $segments[1]    = $this->_strip_hyphens(@$segments[1]);
            
            // 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]);
        }
        
        function _strip_hyphens($string) {
            
            if (strstr($string, '-')) {
                return str_replace('-', '_', $string);
            }
            
            return $string;
        }
        
    }
?>

I just thought I would share since it took a bit of time to figure out. Hope it's helpful.
#2

[eluser]ChazUK[/eluser]
Hi,

Was just trying to figure this out for myself.
Do I create a new file in application/libraries and then copy this code in?

Thanks
#3

[eluser]derrin[/eluser]
Yup, that should do it. It should just override the _validate_request method in CI Router class.

I hope it helps! Let me know if you have any issues.
#4

[eluser]ChazUK[/eluser]
Code:
Fatal error: Class 'MY_Router' not found in ...\system\codeigniter\Common.php  on line 142

This is what I'm getting my config has $config['subclass_prefix'] = 'MY_';
The file is named located in system/application/libraries/MY_Router.php
#5

[eluser]derrin[/eluser]
Hmmm.... Maybe check the permissions on the My_Router.php file? Make sure they are the same as the rest of your CI files.

I can't think of anything else that would cause that issue.
#6

[eluser]derrin[/eluser]
Oh! if your file is named MY_Router.php instead of My_Router.php(lowercase 'y'), that could be the problem.
#7

[eluser]ChazUK[/eluser]
This is what I have.

system/application/config/config.php
Code:
$config['subclass_prefix'] = 'MY_';

system/application/libraries/MY_Router.php
Code:
copied from above

I've tried changing the case of the subclass prefix, as well as changing the case of the class and filename.

And is the error I'm getting.

Code:
_strip_hyphens(@$segments[0]); $segments[1] = $this->_strip_hyphens(@$segments[1]); // 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]); } function _strip_hyphens($string) { if (strstr($string, '-')) { return str_replace('-', '_', $string); } return $string; } }
Fatal error: Class 'MY_Router' not found in \system\codeigniter\Common.php on line 142
#8

[eluser]ChazUK[/eluser]
Found out what it was!

Nothing wrong with the code, except that you are using short PHP tags, which aren't enabled in my php.ini.

Change <? to <?php and all is well again.




Theme © iAndrew 2016 - Forum software by © MyBB