URL array codeigniter

2019-08-15 03:06发布

I'm using codeigniter (newbie at codeigniter).

I have a function getproducts($p1, $p2, $p3) in a controller.

When I call getproducts/0/0/ from my jquery-script (ajax) it works, but I want to call URL like this:

getproducts/0/0/{"0":"13","1":"24"}

it doesn't work. (I get into to google-search-results instead of staying at my local webserver)

I basically want to pass an array to a function in the url somehow when using codeigniter. How should I solve that? Please help :-)

5条回答
【Aperson】
2楼-- · 2019-08-15 03:12

Your browser don't think that that is a URL and navigates to google (thinking that you are searching something), I Think.

The main parts of URLs

A full BNF description of the URL syntax is given in Section 5.

In general, URLs are written as follows:

   <scheme>:<scheme-specific-part>

A URL contains the name of the scheme being used () followed by a colon and then a string (the ) whose interpretation depends on the scheme.

Scheme names consist of a sequence of characters. The lower case letters "a"--"z", digits, and the characters plus ("+"), period ("."), and hyphen ("-") are allowed. For resiliency, programs interpreting URLs should treat upper case letters as equivalent to lower case in scheme names (e.g., allow "HTTP" as well as "http").

Thus, only alphanumerics, the special characters "$-_.+!*'(),", and reserved characters used for their reserved purposes may be used unencoded within a URL.

schemepart     = *xchar | ip-schemepart

See http://www.faqs.org/rfcs/rfc1738.html please.

查看更多
Fickle 薄情
3楼-- · 2019-08-15 03:15

I think a better answer for this question would be to use the inbuilt uri to associative array handler. see http://www.codeigniter.com/user_guide/libraries/uri.html?highlight=uri this stops all that nasty mucking about with config permitted uri characters.

your uri would be: getproducts/p1/0/p2/0/p3/0/p5/13/p6/1/p6/24 and the handler would be something like:

 function get_product()
 {
      $object = $this->uri->uri_to_assoc(4);
 }
查看更多
仙女界的扛把子
4楼-- · 2019-08-15 03:23

I think you should at least adjust the Codeigniter's config about allowed characters in the URL to include curly braces, comma and double quotes :

$config['permitted_uri_chars'] = ',{}"a-z 0-9~%.:_()@\-';

The reason why you end up on Google might however be something else (does not seem to be Codeigniter related)

查看更多
我只想做你的唯一
5楼-- · 2019-08-15 03:32

{"0":"13","1":"24"} should be url encoded.

http://php.net/manual/en/function.urlencode.php

查看更多
地球回转人心会变
6楼-- · 2019-08-15 03:38

You need to use URI class's $this->uri->assoc_to_uri()

Manual wrote,

Takes an associative array as input and generates a URI string from it. The array keys will be included in the string. Example:

$array = array('product' => 'shoes', 'size' => 'large', 'color' => 'red');
$str = $this->uri->assoc_to_uri($array);
// Produces: product/shoes/size/large/color/red
查看更多
登录 后发表回答