Taylor
Taylor

Reputation: 3141

Codeigniter - URI Routes not working

What my current URI looks like: localhost/home/saved, localhost/home/view

What I want it to look like: localhost/saved, localhost/view.

What my home controller looks like:

class Home extends CI_Controller
{
    public function index() {
        //stuff
    }
    public function saved() {
        //stuff
    } 
    public function view() {
        //stuff
    }
}

I want to get rid of the home from the URL so the user can just visit localhost/saved, and other functions in my home without typing the home in the URL.

I tried putting these in the routes.php file but no luck.

$route['home/(:any)'] = "$1";
//that didn't work so I tried putting this:

$route['home/(:any)'] = "/$1";
//that didn't work either.

Any help is highly appreciated. Thanks!

Upvotes: 1

Views: 2383

Answers (2)

Tpojka
Tpojka

Reputation: 7111

Group rule would be:

$route['(:any)'] = 'home/$1';

Set this rule at the end of the routes because (:any) placeholder is greedy and would accept anything from URL if not speciffically pointed. Meaning: if you have controller Product.php with method public function show($id){/*some code*/} and want to approach to, route should be pointed before rule above.

Routes will run in the order they are defined. Higher routes will always take precedence over lower ones.

Docs.

Upvotes: 1

Abdulla Nilam
Abdulla Nilam

Reputation: 38584

Try something like this

$route['saved'] = 'home/saved';
$route['view'] = 'home/view';

Output - www.example.com/saved and www.example.com/view

Upvotes: 1

Related Questions