Question

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

Was it helpful?

Solution

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

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top