在 CodeIgniter 中,如何将数组类型参数从 url 传递给控制器


In CodeIgniter how can i pass an array type argument to a controller from url

//let my controller be C and method be:
    function X($array){}
//And my url to call it is:
    localhost/mysite/C/X/array

好吧,我试过了,但它返回 400-Bad 请求响应。有谁知道该怎么做?快速响应将对我有很大帮助//谢谢

localhost/mysite/C/X/?array=1&&?array=2....

$array = $this->input->get('array'(;

localhost/mysite/C/X/1,2,3,4,5

$array = explode(',' $this->uri->segment(n));
// in app/config/config.php
$config['permitted_uri_chars'] = 'a-z 0-9~%.:,_-';

我在url中数组的变体(/tours/novogodniye-turi/visa=yes;duration=small;transport=4,7,6,2/(

if ( ! function_exists('filter_parse_segment'))
{
    function filter_parse_segment($segment, $merge_to_get = FALSE)
    {   
        if(empty($segment))
        {
            return FALSE;
        }
        $parameters = explode(";", (string)$segment);
        if(empty($parameters))
        {
            return FALSE;
        }
        $parameters_array = array();
        foreach($parameters as $parameter)
        {
            if(empty($parameter))
            {
                continue;
            }
            $parameter = explode("=", $parameter);
            if( ! isset($parameter[0], $parameter[1]) or empty($parameter[0]))
            {   
                continue;
            }
            if(strpos($parameter[1], ','))
            {
                $parameter[1] = explode(",", $parameter[1]);
            }
            $parameters_array[$parameter[0]] =  $parameter[1];      
        }
        if($merge_to_get === TRUE)
        {
            $_GET = array_merge($_GET, $parameters_array);
        }
        return $parameters_array;
    }
}
// --------------------------------------------------------------------
if ( ! function_exists('filter_collect_segment'))
{
    function filter_collect_segment($array, $suffix = '', $remove = array())
    {   
        if(empty($array) || ! is_array($array))
        {
            return '';
        }
        $segment_str = '';
        foreach ($array as $key => $value)
        {
            if(empty($key) || in_array($key, (array)$remove))
            {
                continue;
            }
            if( ! $segment_str == '')
            {
                $segment_str = $segment_str.';';
            }
            if( ! is_array($value))
            {
                $segment_str = $segment_str.$key.'='.$value;
                continue;
            }
            if(empty($value))
            {
                continue;
            }
            $parsed_value = '';
            foreach ($value as $item)
            {
                if( ! $parsed_value == '')
                {
                    $parsed_value = $parsed_value.',';
                }
                if(is_array($item) || empty($item))
                {
                    continue;
                }
                $parsed_value = $parsed_value.$item;
            }
            $segment_str = $segment_str.$key.'='.$parsed_value;
        }
        if($segment_str != '')
        {
            $segment_str = $segment_str.$suffix;
        }
        return $segment_str;
    }
}

// --------------------------------------------------------------------
if ( ! function_exists('filter_key'))
{
    function filter_key($filter_array, $key, $value = NULL)
    {   
        if( ! isset($filter_array[$key]))
        {
            return;
        }
        if($value == NULL)
        {
            return $filter_array[$key];
        }
        if( ! is_array($filter_array[$key]) && $filter_array[$key] == (string)$value)
        {
            return $value;
        }
        if(is_array($filter_array[$key]) && in_array($value, $filter_array[$key]))
        {
            return $value;
        }
        return FALSE;
    }
}

如果你想在漂亮的URL中提供解决方案,那么你必须首先循环数组,然后用一些 - 破折号或+加号连接元素,就像这样;

$array = array(1,2,3,4);
$string = "";
foreach($array as $value){
   $string .= $value."-";
}
$string = rtrim($string, "-");
redirect(base_url()."get_products?param=".$string);

在下一页上,只需获取参数并使用带有 - 破折号的 explode(( 函数再次创建数组。

从您的路线出发

$routes->add('/your-url/(:any)', 'Your-controller::YourFunction');

和你的函数

 YourFunction($segment = null){
  if(!$segment)
      return redirect()->to(base_url());
  }else{
      //on this condition you can separate the url using the the 
      //getSegment() by choosing the index or position from the url
      $segment= $this->request->uri->getSegment(2);
  }
 }

希望对您有所帮助

! 谢谢!

像这样尝试你的网址

 if value you have to pass is
 [1,2,3,2]
 then
 localhost/mysite/index.php/C/X/1,2,3,2

以一种简单的方式,您可以将数组设置为在数组值之间带有一些特殊字符的字符串。

然后在登录页面中,您可以使用特殊字符拆分字符串并再次获取数组。

如果值为 [1,2,3,4

],则使用循环 "1,2,3,4" 进行。

然后传递字符串。

在登录页面中,用","拆分字符串,您将再次获得数组。

希望对您有所帮助。

谢谢

为什么不使用 uri 段进行数组?您可以计算 uri 段并可以使用它们。甚至你可以检查已经有多少个数组。

url.com/1/2/3

http://ellislab.com/codeigniter/user-guide/libraries/uri.html

注意:uri 段不适用于主控制器索引函数,您必须定义除索引之外的另一个函数,例如:我不知道会发生什么,但我认为由于我正在使用的 htaccess 文件,请删除索引.php。

url.com/uri_segment_controller/go/1/2/3