I just learned about routing in CodeIgniter and now I am lil bit confuse.
I have an URL like this : http://localhost/norwin/list_group/get_product_by_group/1
Which is:
list_group is controller,
get_product_by_group is method,
and '1' is passing parameter.
I want to minimize the URL with route. So I can have an URL like :
http://localhost/norwin/group/'group_name'
And this is my code on route.php :
$route['group/(:any)'] = 'list_group/get_product_by_group/$1';
this is my controller :
public function get_product_by_group($group_name)
{
if($this->uri->segment(3))
{
$this->load->database();
$data['product_data'] = $this->group_model->list_product_by_group($group_name);
$this->load->view('fend/list_product', $data);
}
else
{
redirect('list_group');
}
}
And this is my code on view which call the controller :
<?= base_url('group/'. $value->group_name);?>
my problem is : I can not get any result of the route, it always send me to 'list_group'.
Maybe someone here have a good approach to do this.
Thanks for your help.
You are being redirected because $this->uri->segment(3) is not returning anything as your URL http://localhost/norwin/group/'group_name' have only 2 segments.
Codeigniter $this->uri->segment(n); works in the following way.
URL: http://localhost/norwin/group/group_name/some_test
it will return
$this->uri->segment(1); // group
$this->uri->segment(2); // group_name
$this->uri->segment(3); // some_test
you have to change $this->uri->segment(3) to $this->uri->segment(2)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With