views:

54

answers:

2

Hello,

I'm planning on re-creating my lyrics 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

+3  A: 

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
lmz
thank you very much :D
Maikel
A: 

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.

Mike
You still have to pass the name of the artist to the controller function: `$route[':any'] = "artist/lookup/$1";`
captaintokyo
According to the User Guide, you do not. But for the sake of making sure you get what you want, I agree. I'll make the change.
Mike
:any is converted to .+ as routes are basically just regex. If you miss out the () then you cannot use $1. The full route is $route['(:any)'] = "artist/lookup/$1";
Phil Sturgeon