Codeigniter Changing routes makes $this->uri->segment() not working

I've altered routes.php

$route['category']='Home/category';

for making url look like www.website.com/category insteead of www.website.com/Home/category. Since Home is my default controller. but if i am using $this->uri->segment(); inside category function, its not working. this is my controller

class Home extends CI_Controller {

  public function category()
  {
     $value=$this->uri->segmet(3);
  }
}

And my url is www.website.com/category/books I am getting result if I dont alter routes. But by altering routes, I need this to work. Please help me. Thanks

Answers


hello please check segment spelling in your code

class Home extends CI_Controller {

  public function category()
  {
     $value=$this->uri->segmet(3); //wrong 
     $value=$this->uri->segment(3); 
  }
}

You can debug what you have there by:

var_dump($this->uri->segment_array());

this will give you array of all segments in URI.

Also you can try to debug with this method:

var_dump($this->uri->rsegment_array());

this will give you array of all routed segments in URI

Respectivelly, you can use $this->uri->segment() or $this->uri->rsegment() what ever you find more appropriate for your application.


First of all you need to load URL library and than if your URL is:

www.website.com/category/books

And you want to get books from URL than segment should be:

$value=$this->uri->segment(2); //books

My URL libraries are in autoload. Anyway I solved it. I just configured my routes like this $route['category/(:any)']='Home/category/$1'


Need Your Help

Cannot change window size and position between hide and show

wpf resize window flicker sizetocontent

In my scenario I have a window which I want to use to display on two different ways. One way when the window has WindowState = WindowState.Normal; and the other way where the window has Normal or