显示带有类别的产品

时间:2016-06-01 06:45:59

标签: php codeigniter

我在codeigniter中遇到uri段的麻烦我正在寻找开发产品和产品的详细信息,所以我为它创建了一个控制器,并传递了产品名称的参数,以便在url中显示产品名称,这样我的url就会变成这样的< / p>

  

localhost / project_name / products / product_name

这部分对我来说很好但我很困惑如果我想显示为产品类别和产品名称,就像我试图让我的网址像这样

  

localhost / project_name / products / category_name / product_name

所以当我传入类别名称时,我将如何执行此操作,它显示我找不到404找不到您请求的页面。有时会显示直接产品,有时候如果用户通过类别浏览产品详细信息,那么类别名称将在网址中传递,以及这些部分的工作方式如何?我对此很困惑

这是我的产品控制器

public function products($product_name) {
        if($this->uri->segment(3)) {
            $category = ucwords(str_replace('-', " ", $product_name));
            $product  = ucwords(str_replace('-', " ", $this->uri->segment(3)));
        } else {
            $product  = ucwords(str_replace('-', " ", $product_name));
        }

        $page_data = $this->get_data->get_Data($product, 'name', 'products');
        $data['title'] = 'Products - ' . $page_data->row()->name;
        $data['body_class'] = 'poductsdetails';

        $this->load->view('includes/header.php', $data);
        $this->load->view('templates/navigation.php');

        $data['content'] = array(
                'name'        => $page_data->row()->name,
                'price'       => $page_data->row()->price,
                'description' => $page_data->row()->description,
                'picture'     => $page_data->row()->picture,
                'category'    => $page_data->row()->category,
            );

        $this->load->view('products/details', $data);
        $this->load->view('templates/footer-form.php');
        $this->load->view('includes/footer.php');
    }

这是我创建的路线

$route['default_controller'] = 'home';
$route['admin']              = 'admin/index';
$route['admin/([a-z])']      = 'admin/$1';
$route['products/(:any)']    = 'home/products/$1';
$route['products/(:any)']    = 'home/products/$1/$2';
$route['products/(:any)']    = 'home/products/$1/$2/$3';
$route['(:any)']             = 'home/page/$1';
$route['(:any)']             = 'home/page/$1/$2';

2 个答案:

答案 0 :(得分:6)

您刚刚在路线中传递了一个参数,原因是它无法找到

像这样改变你的路线

$route['products/(:any)']    = 'home/products/$1';
$route['products/(:any)/(:any)']    = 'home/products/$1/$2';
$route['products/(:any)/(:any)/(:any)']    = 'home/products/$1/$2/$3';

它会起作用。

答案 1 :(得分:0)

如果category_name是在此控制器中定义的函数,则尝试此

$route['products/category_name/(:any)'] = 'products/category_name/$1';