URL array codeigniter

做~自己de王妃 提交于 2020-01-06 02:24:05

问题


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 :-)


回答1:


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)




回答2:


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.




回答3:


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

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




回答4:


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);
 }



回答5:


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


来源:https://stackoverflow.com/questions/17015096/url-array-codeigniter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!