2013-10-25 88 views
0

我正在通过API在我的网站上嵌入国旗图像&其他几个。自定义路由不工作| Codeigniter路由

我在3个参数取即

  1. 国家(国家名称 - ISO代码或全名)
  2. 尺寸(图像尺寸)
  3. 类型(样式,如平面标志,闪亮的圆国旗等)

现在,一切设置正确,但坚持处理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"; 

回答

0

我在编写自定义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"; 

如果你想从你的标志类,它看起来像你这样做,你不想航线运行指数法欢迎上课。但是,目前,你是。您的路线应该如下所示:

$route['flag/(:any)/(:num)/(:any)'] = "flags/index"; 

这样Codeigniter会从您的标志类运行索引方法。您不必担心路线中的国家,大小或风格/类型。最好的办法会有使用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

让我知道如果你需要更多的帮助,或者如果这有助于解决您的问题。