我打算在CodeIgniter中重新创建我的歌词网站。 此刻,我的方式把它建立是这样的:
example.com/artistname
和example.com/anotherartist
CodeIgniter网址帮助
我也有example.com/contact
和example.com/request
等。
我可以得到它是example.com/artist/artistname
,但我d真的很想让用户记住这些URL。
任何人都可以帮我解决这个问题吗?
感谢, Maikel
我打算在CodeIgniter中重新创建我的歌词网站。 此刻,我的方式把它建立是这样的:
example.com/artistname
和example.com/anotherartist
CodeIgniter网址帮助
我也有example.com/contact
和example.com/request
等。
我可以得到它是example.com/artist/artistname
,但我d真的很想让用户记住这些URL。
任何人都可以帮我解决这个问题吗?
感谢, Maikel
在application/config/routes.php
尝试:
$route['contact'] = 'contact'; // /contact to contact controller
$route['request'] = 'request'; // /request to request controller
$route['(.*)'] = 'artist/display/$1'; // anything to artist controller, display method with the string as parameter
通过这里的CodeIgniter用户指南:http://codeigniter.com/user_guide/general/routing.html
可以重新映射任何东西(:any
)到您的artist
控制器。从那里,您可以将contact
,request
等重新映射到它们各自的控制器/函数,或者您可以使用您的构造函数来检查它们并调用正确的函数。例子:
使用URI路径:
$route['contact'] = "contact";
$route['request'] = "request";
... // etc...
$route['(:any)'] = "artist/lookup/$1"; // MUST be last, or contact and request will be routed as artists.
使用您的构造函数:
public function __construct($uri) {
if ($uri == "contact") {
redirect('contact');
} elseif ($uri == "request") {
redirect('request');
}
}
这种方法,但是,可能会导致一个无限循环。我不会建议它,除非你的contact
和request
功能在同一个控制器。那么你可以用$this->contact()
或$this->request()
而不是重定向来打电话给他们。
非常感谢:D – Maikel 2010-09-13 03:21:23