【问题标题】:CodeIgniter URL Query String ManipulationCodeIgniter URL 查询字符串操作
【发布时间】:2017-03-31 11:37:40
【问题描述】:

我有一个使用 CodeIgniter 2.3 构建的电子商务网站。

请建议我如何按给定顺序操作 URL:-

  1. 假设某人搜索“手机”。 URL 必须是 xyz.com/search/mobile-phone,它显示我们数据库中的所有手机。 我已经知道 Search 控制器并使用 Routing 从 URL 中排除函数名称

  2. 接下来,Person 使用移动电话公司的名称进行过滤。假设他选择“Apple”。 网址必须变为 xyz.com/search/mobile-phones/apple

  3. 接下来,Person 再次按条件过滤,即“新”或“翻新”。 网址必须变为 xyz.com/search/mobile-phones/apples/new

  4. URL 必须是功能性 URL,这样,如果我将 URL xyz.com/search/mobile-phones/apples/new 传递给外部某人,他/她应该能够看到相同的结果集作为搜索和过滤之后的人。

我不是技术人员,所以我可能无法从技术上解释我的问题,但请放心,我的技术人员会阅读您的建议。

更多详情

我们已经在使用 URI 分段、路由和发布方法。但是这样我们就面临着上面提到的第 4 点的问题

【问题讨论】:

  • 你可以使用 CodeIgniter 路由来解决这个问题。请让您的技术人员调查一下。
  • 可以使用application/config/routes.php中的routes.php

标签: php codeigniter post get query-string


【解决方案1】:

假设您的控制器文件名为 Search.php,您可以使用这样的控制器

将此路由添加到 config/route.php 到 this $route['search/(.+)']='search/index/$1';

然后在你的搜索php中指定索引函数

function index($search_type,$brand=null,$category=null){
    //write something here
}

所以当你访问
xyz.com/search/mobile-phones/apple
该路线也将重定向此 xyz.com/search/index/mobile-phones/apple
手机存储到$search_type,苹果存储到$brand

如果您搜索 xyz.com/search/mobile-phones/apple/new
路由也会重定向这个
xyz.com/search/index/mobile-phones/apple
手机将存储到$search_type, 苹果将​​存储到$brand, 新的将传递给$category

路由参考
https://www.codeigniter.com/userguide3/general/routing.html

Controller参数传递参考
https://www.codeigniter.com/userguide3/general/controllers.html#passing-uri-segments-to-your-methods

【讨论】:

    【解决方案2】:

    您可以通过为控制器的每个方法调用提供路由来完成这些工作 像这样的方法 - :

    例如,您的控制器是 移动.php

    defined('BASEPATH') OR exit('No direct script access allowed');
    class Mobile extends CI_Controller {
    public function getMobiles()
    {
        $uri_3 = $this->uri->segment(3);
        $uri_4 = $this->uri->segment(4);
    
        if($uri_3 !="" &&  $uri_4 =="" ){
            //make search by mobile brand ,like apple
            echo $uri_3;die;
        }else if($uri_3 !="" & $uri_4 !=""){
            echo $uri_3.'-'.$uri_4; die;
            //make search by brand and new , like apple/new
        }else{
            //make search for all mobile brand by default
            echo 'All';die;
        }
      }
    }
    

    并且route.php文件应该是这样的-:

    $route['search/mobile-phone'] = 'Mobile/getMobiles';
    $route['search/mobile-phone/(:any)'] = 'Mobile/getMobiles';
    $route['search/mobile-phone/(:any)/(:any)'] = 'Mobile/getMobiles';
    

    网址应该是这样的-:

    http://localhost:90/ci_test/index.php/search/mobile-phone/apple/new
    

    如果你没有在 url 中使用 index.php,那么你的 uri 段将会改变,所以要小心

    http://localhost:90/ci_test/index.php/search/mobile-phone/apple/new
    O/P- apple-new
    
    http://localhost:90/ci_test/index.php/search/mobile-phone/apple/
    O/P- apple
    
    http://localhost:90/ci_test/index.php/search/mobile-phone/
    O/P- All 
    

    【讨论】:

      【解决方案3】:

      例如: 您的网址是:xyz.com/search/mobile-phones/apple/condition/new

       $segment_array=$this->uri->segment_array();
       $mobile_brand=array_search('mobile-phones',$segment_array);    
              if($mobile_brand !==FALSE && $this->uri->segment($mobile_brand+1)!='')
              {
                  $mobile_brand = $this->uri->segment($mobile_brand+1); //apple
              }
      
       $condition=array_search('condition',$segment_array);   
              if($condition !==FALSE && $this->uri->segment($condition+1)!='')
              {
                  $condition = $this->uri->segment($condition+1); //new
              }
      

      您可以通过检查 $mobile-brand 和 $condition 来限制结果来跟进您的编码。 这样就不会和其他参数冲突了,传递参数采用key和value的方式。

      试着让我知道你的输出。

      【讨论】:

        【解决方案4】:

        我将在客户端使用脚本来回答这个问题。

        1.假设一个人搜索“手机”。 URL 必须是 xyz.com/search/mobile-phone,它显示我们数据库中的所有手机。我已经知道 Search 控制器并使用 Routing 从 URL 中排除函数名称

        只需获取输入值并将其传递给脚本。

        <script>
        
        $('#search').keyup(function (e) {
        
        if(e.keyCode==13)
        {
            var search=$('#search').val();
        
            window.location.href = 'xyz.com/search/'+search;
        }
        });
        
        </script>
        

        2。接下来,人员使用移动电话公司的名称进行过滤。假设他选择“Apple”。 URL 必须变为 xyz.com/search/mobile-phones/apple

        当这个人点击苹果时,你就开始写了。

        onclick="javascript:window.location.href='xyz.com/search/&lt;?php echo $this-&gt;uri-&gt;segment(3).'/Apple'; ?&gt;';"

        3。接下来,人员再次按条件过滤,即“新”或“翻新”。 URL 必须变为 xyz.com/search/mobile-phones/apples/new

        假设新的或翻新的选择框。

        $('#select').change(function(e) { 
        
                var select=$('#select').val();
        
                window.location.href = 'xyz.com/search/<?php echo $this->uri->segment(3)."/".$this->uri->segment(4); ?>'+select;
        
         });
        

        4. URL 必须是功能性 URL,这样,如果我将 URL xyz.com/search/mobile-phones/apples/new 传递给外部某人,他/她应该能够看到与后来者相同的结果集搜索和过滤。

        在控制器中,您可以使用 uri 段获取值。

        $this->uri->segment(3)
        $this->uri->segment(4)
        $this->uri->segment(5)
        

        让查询成为你的愿望并返回结果。

        上述网址适用于所有人,如果您使用参数进行查询结果。

        我没有写任何验证,所以无论用户输入是否为空,都进行验证并将其传递给 url。

        希望这会有所帮助:)

        【讨论】:

          猜你喜欢
          • 2019-05-15
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多