【问题标题】:Shorten URL by removing empty GET variables and simplifying variable names通过删除空 GET 变量和简化变量名称来缩短 URL
【发布时间】:2015-04-16 21:11:44
【问题描述】:

我正在开发一个在提交 GET 表单 后组成 URL 的网站。表单值作为变量数组传递,必须定义其中至少一个变量才能在数据库上进行搜索。我想通过删除空的表单元素来缩短 URL,并通过简化变量名使其更加用户友好。

目前,URL 看起来像这样(只是有更多的变量):

http://localhost/example/search?FormName[name]=lorem+ipsum&FormName[id]=&FormName[age]=&yt0=Search

我的目标是让它看起来像这样:

http://localhost/example/search?name=lorem+ipsum

为此,我有以下问题:

  • 我了解到,使用 GET 方法时,仅使用 PHP 无法删除空表单元素,因为这是 html 表单的标准行为。有没有办法用来自 yii 的 urlManager 做到这一点?

  • 我能否在不更改变量名称的情况下将“FormName[name]”替换为“name”等更短的名称,例如,使用正则表达式?

  • 最后但并非最不重要的一点是:“yt0=Search”是什么意思?如何从 URL 中删除它?

任何帮助将不胜感激。

【问题讨论】:

    标签: php forms yii get


    【解决方案1】:
    $('#your-form').submit(function () {
        var data = $(this).serializeArray().filter(function (item) {
            return !!item.value;
        });
        var param = jQuery.param(data);
        window.location = this.action + (param ? "?" + param : "");
        return false;
    });
    

    【讨论】:

      【解决方案2】:

      简单的方法,如果 jQuery 是一个选项:

      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
      <script type="text/javascript">
          (function($) {
              $('form').submit(function() { // ## Clean GET on Submit
                  $('form input').each(function() { // ## Check each Input
                      if ($(this).val().length == 0) { // ## If Empty
                          $(this).attr('disabled', true); // ## Disable Input
                      }
                  });
              });
          })(jQuery);
      </script>
      

      【讨论】:

      • $('form')$('form input') 替换为 $('.cleanthisclass')$('.cleanthisclass input') 以仅针对具有特定类的表单。
      【解决方案3】:

      我建议您使用以下解决方案: 首先,您需要使用 POST 方法定义 html 表单:

      <form method="post" action="/example/getSearchTerms">
          <input type="text" name="name" value="lorem ipsum">
          <button type="submit">Search</button>
      </form>
      

      其次,您需要在ExampleController 中定义getSearchTerms 操作:

      public function actionGetSearchTerms()
      {
          $this->render(Yii::app()->baseUrl.'/example/search/'.$_POST['name']);
      }
      

      然后,您需要定义主要搜索操作:

      public function search($name)
      {
          //do search operation here.
      }
      

      最后,你需要添加一个 url-manager 规则:

      "example/search/<name>"=>"example/search"
      

      在这个解决方案中,getSearchTerms action 负责接收用户输入的文本,然后将值传递给 search action。现在您的网址可以是 http://localhost/example/search/sampleText 。请注意,如果您愿意,可以跳过添加 url-manager 规则。在这种情况下,您的网址必须类似于http://localhost/example/search/name/sampleText。实际上,我们可以通过添加 url-manager 规则从 url 中删除“name”部分。

      【讨论】:

      • 感谢您的回答,但 GET 是应用程序的要求,我无法更改。
      【解决方案4】:

      参数名称来自表单字段的name 属性。

      所以要对name=lorem+ipsum 进行表单查询,输入必须如下所示:

      <form method="get" action="/example/search">
          <input type="text" name="name" value="lorem ipsum">
          <button type="submit">Search</button>
      </form>
      

      您应该查看name 属性,我猜它们是由您用来创建代码的某些代码生成的?空查询参数来自表单中的其他输入字段。如果您想完全控制查询字符串,请手动创建表单。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-02-07
        • 2023-04-08
        • 2014-11-29
        • 2012-12-22
        • 2010-09-25
        • 1970-01-01
        • 2014-02-20
        • 2011-10-01
        相关资源
        最近更新 更多