phpcodeigniterurl

changing the url into dash instead of underscore using Codeigniter


Hi im using codeigniter framework because its really cool when it comes to MVC thing. I have a question regarding in the url thing. Usually when saving a controller, lets say in the about us page it has to do something like this About_us extends CI_Controller. When that comes to the url thing it goes like this test.com/about_us. I want that my url is not underscore. I want to be dashed something like this test.com/about-us. How will i able to use the dash instead of using underscore???

any help is greatly appreciated thanks!


Solution

  • You will just have to create routes for your pages, as far as I am aware, there is no config directive to change the separating character, or replace '-' with '_' in CI_Router, (probably wouldn't be too hard to add this).

    To allow for '-' instead of '_':

    Create a file in application/core/MY_Router.php

    Change 'MY_' to whatever value is specified in your config directive: $config['subclass_prefix']

    insert code:

    <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    
    class MY_Router extends CI_Router {
      function _set_request($segments = array()) {
        if (isset($segments[0]))
          $segments[0] = str_replace('-','_',$segments[0]);
    
        if (isset($segments[1])) {
          $segments[1] = str_replace('-','_',$segments[1]);
        }
    
        return parent::_set_request($segments);
      }
    }