blurry
blurry

Reputation: 1

CodeIgniter and friendly urls

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.

Upvotes: 0

Views: 1252

Answers (3)

Anther
Anther

Reputation: 1844

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);
    }
}

Upvotes: 1

icchanobot
icchanobot

Reputation: 3343

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.

Upvotes: 1

Henry Merriam
Henry Merriam

Reputation: 824

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

Upvotes: 0

Related Questions