Gayathri
Gayathri

Reputation: 333

Url routing in Codeigniter 2

I have url like http://example.com/blog/viewpost/1 and i want to change the url like http://example.com/1. I am using codeigniter 2.x framework so i have changed the routes.php like,

$route['default_controller'] = "blog";
$route['/(:num)'] = 'blog/viewpost/$1';
$route['404_override'] = '';

My controller blog.php,

public function viewpost($id)
    {

        $view['text'] = $this->blog_model->get_post_by_id($id);
        $view['maincategory'] = $this->blog_model->get_main_category();
        foreach ($view['text'] as $text){
            # code...
            $count = $text->views;
            $count = $count+1;
            $this->blog_model->popular_view($id,$count);   
        }
        $this->load->view('readmore',$view);
    }

when i executing the program, the url is not changing. I have done lots of projects in codeigniter framework but I've never done the url routing before. So i hope someone will help on this issue.

Upvotes: 1

Views: 2243

Answers (1)

Kausha Mehta
Kausha Mehta

Reputation: 2928

You can route like $route['{default_controller}/{default_method}/(:num)'] = "{original_controller}/{original_method}/$1"; in the file '{root}/system/application/config/routes.php'. For more you can visit http://codesamplez.com/development/codeigniter-routes-tutorial

=======================

Add the below code on your "Blog" controller.

public function _remap($method, $params = array()) {
    if ($method == 'viewpost') {
        $this->$method($params[0]);
    } else {
        $this->default_method();
    }
}

Upvotes: 1

Related Questions