Welcome Guest, Not a member yet? Register   Sign In
Remove Controller Name From URL - Solution
#1

[eluser]loosetops[/eluser]
Here is a way to remove the default controller name from the URLS

Say you have a default controller (home in our case), your URLs will be of this format

Code:
http://website.com/
http://website.com/home/about
http://website.com/home/contact
http://website.com/home/faq
http://website.com/home/resources

To get rid of the default controller name we use the routes file in the config folder. For the above example this would work

Code:
$route['^(about|contact|faq|resources)(/:any)?$'] = "home/$0";

What it does, is it checks if the any of the requested methods match the methods in your default controller. If they do it routes them to the default controller. You have to include all the methods in the default controller class. This is by re-routing by inclusion.

You can now use these URLS
Code:
http://website.com/
http://website.com/aboutus
http://website.com/contact
http://website.com/faq
http://website.com/resources

Another solution would be by exclusion e.g.
Code:
$route['^(?!users|blog|controller3|controller4).*'] = "home/$0";

In the above solution we use names of other controllers and we re-route anything that doesn't match to the default/home controller. You have to exclude the controller names so that their methods/URLS are not re-routed

I prefer the first one.
#2

[eluser]Denarius[/eluser]
If that's the target url style, wouldn't you be better off defining home as the default controller and creating the other methods as distinct controller classes i.e class About extends Controller etc? Possibly having more and smaller individual controller classes is a better way to go? I'm new to CodeIgniter so don't take what I'm saying as gospel.

Clever bit of routing though, I'm still struggling with that bit.




Theme © iAndrew 2016 - Forum software by © MyBB