Use hyphen in CodeIgniter URL

I normally like to use hyphens than underscores in URL. It looks more easier to read I think. But in CodeIgniter normally we can’t use hyphens in URL as hyphens isn’t allowed in function name or class name.

To solve this in CodeIgniter, so the correct function name is found from the uri segment, only one simple change needs to be made.

There are two easier way. First one is:
In system/libraries/Router.php find line 153 and change this line

$segments = $this->_validate_request($segments);

to

$segments = $this->_validate_request(str_replace(“-”, “_”, $segments));

Some people don’t want to edit core codes. In that case, we can use an alternative way.

A better solution is to create a MY_Router.php file in your /application/core directory. In that file you can have the following code:

This is a nicer solution, as it doesn’t alter any segments beyond the controller and method, which is generally what people are after.

Leave a Reply

Your email address will not be published. Required fields are marked *