我正在开发一个API,通过它我可以在我的网站上嵌入国旗的图片&其他几个。
我要考虑三个参数
现在,让所有的设置都正确,但只限于处理URI。
Controller -> flags.php
Function -> index()
我现在拥有的是:
http://imageserver.com/flags?country=india&size=64&style=round
我想要什么
http://imageserver.com/flag/india/64/round
我看了几篇文章,做了这条路,但都是失败了。
$route['flag/(:any)/(:num)/(:any)'] = "welcome/index/country/$1/size/$2/style/$3";
$route['flag/(:any)/(:num)/(:any)'] = "welcome/index/$1/$2/$3";
$route['flag/(:any)/(:num)/(:any)'] = "welcome/index?country=$1&size=$2&style=$3";
发布于 2014-01-16 17:22:59
在编写自定义cms的过程中,我也遇到了路线问题。阅读你的问题,我看到几个问题,很可能是你正在寻找的答案。
首先,让我们看看您尝试过的路线:
$route['flag/(:any)/(:num)/(:any)'] = "welcome/index/country/$1/size/$2/style/$3";
$route['flag/(:any)/(:num)/(:any)'] = "welcome/index/$1/$2/$3";
$route['flag/(:any)/(:num)/(:any)'] = "welcome/index?country=$1&size=$2&style=$3";
如果希望从标志类运行index方法(它看起来是这样的),则根本不想路由到迎宾类。然而,目前,你是。你的路线应该是:
$route['flag/(:any)/(:num)/(:any)'] = "flags/index";
这样,Codeigniter将从标志类中运行index方法。您不必担心路线中的国家、大小或样式/类型。最好的选择是像这样使用URI段函数:
$country = $this->uri->segment(2); //this would return India as per your uri example.
$size = $this->uri->segment(3); //this would return 64 as per your uri example.
$style = $this->uri->segment(4); //this would return round as per your uri example.
然后,您可以使用这些变量查询数据库,并获得正确的标志或其他需要对它们进行的操作。
因此,我要重申我的回答,并进一步解释一下为什么:
当前的路由正在运行欢迎控制器/类和该控制器/类的索引函数/方法。很明显,这不是你想要的。因此,您需要确保您的路线指向正确的控制器和功能,就像我上面所做的那样。URI的额外片段不需要在路由声明中,因此您只需使用uri_segment()函数来获取每个段的值,并对它们执行所需的操作。
希望这能帮到你。我可能没有找到我的问题的答案,但至少我可以为其他人提供一个答案。如果这让您感到困惑,请查看http://ellislab.com/codeigniter/user-guide的用户指南。您需要的主要链接是:
http://ellislab.com/codeigniter/user-guide/libraries/uri.html
和
http://ellislab.com/codeigniter/user-guide/general/routing.html
如果你需要更多的帮助,或者这是否有助于解决你的问题,请告诉我。
https://stackoverflow.com/questions/19596463
复制相似问题