CodeIgniter: URL routing for Codeigniter

Hey guys, I just learned about routing in CodeIgniter and now I'm a little confused.

I have a URL like this: http://localhost/norwin/list_group/get_product_by_group/1

which the:

  • list_group is a controller,

  • get_product_by_group is a method

  • and '1' the parameter is passed.

I want to minimize the url using a route. So I might have a url:

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 that calls the controller:

<?= base_url('group/'. $value->group_name);?>

      

my problem: I cannot get any route result, it always sends me to 'list_group'.

Maybe someone has a good approach for this.

Thank you for your help.

+3


source to share


1 answer


You are redirected because it $this->uri->segment(3)

returns nothing as your URL http://localhost/norwin/group/'group_name'

only has 2 segments.

Codeigniter $this->uri->segment(n);

works like this.

URL: http: // localhost / norwin / group / group_name / some_test



he will return

$this->uri->segment(1); // group
$this->uri->segment(2); // group_name
$this->uri->segment(3); // some_test

      

you need to change $this->uri->segment(3)

to$this->uri->segment(2)

+2


source







All Articles