开发者

CodeIgniter URLs help

开发者 https://www.devze.com 2023-01-16 01:58 出处:网络
I\'m planning on re-creating my lyri开发者_C百科cs website in CodeIgniter. At the moment, the way I have it set-up is like this:

I'm planning on re-creating my lyri开发者_C百科cs website in CodeIgniter. At the moment, the way I have it set-up is like this:

example.com/artistname and example.com/anotherartist

I also have example.com/contact and example.com/request etc..

I can get it to be example.com/artist/artistname, but I'd really like to keep it simple for the user to memorize the urls.

Can anyone help me out with this?

Thanks, Maikel


In application/config/routes.php try:

$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


Via the CodeIgniter User Guide here: http://codeigniter.com/user_guide/general/routing.html

You can remap anything (:any) to your artist controller. From there, you can remap contact, request, etc. to their respective controllers/functions or you can use your Constructor to check for those and call the correct function. Examples:

Using URI Routing:

$route['contact'] = "contact";
$route['request'] = "request";
... // etc...
$route['(:any)'] = "artist/lookup/$1"; // MUST be last, or contact and request will be routed as artists.

Using your Constructor:

public function __construct($uri) {
    if ($uri == "contact") {
        redirect('contact');
    } elseif ($uri == "request") {
        redirect('request');
    }
}

This method, however, could result in an infinite loop. I would not suggest it, unless your contact and request functions were in the same controller. Then you could just call them with $this->contact() or $this->request() instead of the redirect.

0

精彩评论

暂无评论...
验证码 换一张
取 消