0

I currently have a set of urls which are derived from their controller names but they're not very url friendly.

For example, is there anyway I change:

example.com/admin/news_manager/add_article

to

example.com/admin/news-manager/add-article

Any help would be greatly appreciated! Thank you.

blurry
  • 1
  • 1
  • Related: [http://stackoverflow.com/questions/2428134/codeigniter-routes-regex/2432405#2432405](http://stackoverflow.com/questions/2428134/codeigniter-routes-regex/2432405#2432405) – Wesley Murch Apr 15 '11 at 19:18

3 Answers3

1

Here's what I have, put this in your application/core folder as MY_Router.php

It will convert all of the -'s in your url to _'s.

    <?php

class MY_Router extends CI_Router { 
    function set_class($class) 
    {
        $this->class = $this->replace_underscores($class);
    }

    function set_method($method)
    {
        $this->method = $this->replace_underscores($method);
    }

    private function replace_underscores($string){
        return str_replace('-', '_', $string);
    }

    function _validate_request($segments)
    {
        $this->segments = $segments;
        if(empty($this->segments) || $this->controller_is_in_root_folder())
            return $this->segments;

        if ($this->controller_is_in_a_subfolder())
        {
            $this->segments =  $this->set_directory_and_remove_from_array();

            if ($this->at_least_one_segment() > 0 && !$this->default_controller_is_in_subfolder())
            {
                    show_404($this->fetch_directory().$this->segments[0]);
            }
            else
            {
                $this->set_class($this->default_controller);
                $this->set_method('index');

                if (!$this->default_controller_is_in_subfolder())
                {
                    $this->directory = '';
                    return array();
                }
            }
            return $this->segments;
        }
        else
            show_404($this->segments[0]);
    }

    private function at_least_one_segment(){
        return count($this->segments) >= 1;
    }

    private function controller_is_in_a_subfolder(){
        return is_dir(APPPATH.'controllers/'.$this->segments[0]);
    }

    private function controller_is_in_root_folder(){
        return file_exists(APPPATH.'controllers/'.str_replace('-', '_', $this->segments[0]).EXT);
    }

    private function set_directory_and_remove_from_array(){
        $this->set_directory($this->segments[0]);
        return array_slice($this->segments, 1);
    }

    private function default_controller_is_in_subfolder(){
        return file_exists(APPPATH.'controllers/'.$this->fetch_directory().str_replace('-', '_', $this->segments[0]).EXT);
    }
}
Anther
  • 1,834
  • 12
  • 13
1

If its just that one function you can open applications/config/routes.php and add a line something like this:

$route['^admin/news-manager/add-article'] = $route['admin/news_manager/add_article'];

depending on what your other urls are you could come up with a more generic rule.

icchanobot
  • 3,323
  • 27
  • 37
0

You can use URI routing. See the URI routing page in the CodeIgniter docs.

Henry Merriam
  • 834
  • 6
  • 20