【问题标题】:codeigniter url routing with query string带有查询字符串的codeigniter url路由
【发布时间】:2017-08-20 08:53:43
【问题描述】:

我的项目中有 2 个网址

http://example.com/one.html

http://example.com/one.html?param1=value&param2=value

在路由文件中我有如下规则

$route['search/one?(:any)'] = 'advance_search/books';
$route['search/one'] = 'advance_search/index';

当我用查询字符串调用第二个 url 时,它仍然调用索引方法。任何人都可以帮助解决问题。

【问题讨论】:

  • ? 是正则表达式的一部分
  • $route['search/one/?(:any)'] = 'advance_search/books';也尝试过这样但不起作用
  • 希望我的回答有帮助不要忘记接受它

标签: codeigniter url routes


【解决方案1】:

试试你的路线

http://example.com/index.php/controller_name?param1=value&param2=value

控制器必须是 php 文件

确保类和文件名的第一个字母是大写的。

控制器/One.php

<?php

class One extends CI_Controller {

    public function index() {

      echo $this->input->get('param1');


    }
}

如果您需要删除 index.php,请尝试其中的一些 htaccess

https://github.com/wolfgang1983/htaccess_for_codeigniter

【讨论】:

    【解决方案2】:

    我正在为类似的事情苦苦挣扎,发现自己解析 URL 提供了最大的便利性。

    public function search() {
        $params = array();
        $url = parse_url($_SERVER['REQUEST_URI']);
        parse_str($url['query'], $params);
        // $params now is an array containing the name-value pairs of the querystring. 
        echo("<pre>".print_r($params ,true)."</pre>\n");exit; // for debugging, 
    }
    

    这和路线一起去

    $route['search/(.+)'] = 'search/search/$1';
    

    这会将所有参数转储到搜索控制器的搜索功能中,并允许您根据需要进行处理。

    【讨论】:

      猜你喜欢
      • 2014-03-05
      • 1970-01-01
      • 2012-04-01
      • 2011-01-16
      • 2015-11-04
      • 2020-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多