【问题标题】:CodeIgniter : Routing URL on CodeigniterCodeIgniter : Codeigniter 上的路由 URL
【发布时间】:2015-05-03 06:38:27
【问题描述】:

我刚刚学习了 CodeIgniter 中的路由,现在我有点困惑。

我有一个这样的网址:http://localhost/norwin/list_group/get_product_by_group/1

这是:

  1. list_group 是控制器,

  2. get_product_by_group是方法,

  3. '1' 是传递参数。

我想用路由最小化 URL。所以我可以有一个像这样的网址:

http://localhost/norwin/group/'group_name'

这是我在 route.php 上的代码:

$route['group/(:any)'] = 'list_group/get_product_by_group/$1';

这是我的控制器:

    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');
            }

    }

这是我调用控制器的视图代码:

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

我的问题是:我无法得到路由的任何结果,它总是将我发送到'list_group'.

也许这里有人有一个很好的方法来做到这一点。

感谢您的帮助。

【问题讨论】:

  • 路由配置没问题。您遇到了什么错误?
  • @KamranAdil : 结果总是将我重定向到'list_group'
  • 它正在重定向您,因为您的 URL 中没有 $this-&gt;uri-&gt;segment(3)。您只有两个段“组”和“组名”。将其更改为$this-&gt;uri-&gt;segment(2)
  • 谢谢,我没有意识到,对不起

标签: php codeigniter routes uri


【解决方案1】:

您被重定向是因为 $this-&gt;uri-&gt;segment(3) 没有返回任何内容,因为您的 URL http://localhost/norwin/group/'group_name' 只有 2 个段。

Codeigniter $this-&gt;uri-&gt;segment(n); 的工作方式如下。

网址:http://localhost/norwin/group/group_name/some_test

它会返回

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

您必须将$this-&gt;uri-&gt;segment(3) 更改为$this-&gt;uri-&gt;segment(2)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-12
    相关资源
    最近更新 更多