CodeIgniter is great because you can separate your content from your styling with controllers, models, and views. Another great thing is when you create a website with CI, you automatically use friendly URLs (if you use an .htaccess file). If you have a bunch of unrelated pages however, you might wonder how you can keep those URLs as tiny as possible without having thirty different controllers for each page. For example, I have a CI application with a home controller that has about 20 different functions inside of it. To access each one of these functions, I would normally visit http://localhost/home/contact or http://localhost/home/about_us. Since we don’t really need home in the URL, I have found a way to remove it from the URL, still allowing other controllers to function normally. Here’s how to do it:
Open up your routes.php file inside the application/config folder in your CI app and add the following line to the end of the routes.php file:
$route['^(?!ezstore|ezsell|login).*'] = "home/$0";
This line uses a regular expression that means, If a visitor goes to any url EXCEPT ezstore, ezsell, or login, redirect them to the home controller, and the function inside the home controller ($0). This way, every time we call a function from the home controller, such as contact, about_us, services, etc., we can snip home out of the URL and keep our URLs short and pretty.
The end!
It’s really a great tip! But it could be better if, instead of manually write the exceptions (name of the controllers), it was possible to read the controllers directory and automatically fill the names…
Do you think it’s possible? Any idea how?
You would have to write a loop before the routes come into play to identify all of the different functions inside the classes. Then, you would have to make sure none of them are named the same or choose a default route if functions are named the same within multiple controllers. Seems like a lot of work! There may be a better way…
You may be able to just loop through all of your classes and try searching for a function that exists. That would be the easiest route I think. Then, you would just have to specify the names of the controller classes.
Уроки счастья или как встретить свою любовь на Западе
Расскажет и научит вас вебсайт http://www.mydatelove.com
Thanks for this!just what I needed!
Wow, I was really stuck on my project until I found this, exactly what I needed.
Great tip!
i will play with routes and then post my experience.
comment3, little old ladies in car, bpa, little old ladies in car clipart, zqwzia, little old ladies in car graphics, 556, best car mileage gas miles, qcnoze, little old ladies in car pictures, %-OOO, auto car magazine, ffm, people in cars clipart, 87469, how fast can gas power rc cars go, 466, prevent car theft, 8-OOO, rent a car anchorage, puvf, rent a car anchoragh, 8-((, hiding a car from repossession in texas, mctg, consumer report on car reliability and p, ordu, malibu car horn, qwtk, oem car horn, qor, i forget my car, :]], custom car graphics, wdv, price of a new car, vakmq, elise carlies, gcqhib, denver ski package car rental, fgjmb, car radio programing, kska, car mechanics in newmarket suffolk, dcncid, car radio rover, 3692, car mechanics n newmarket suffolk, %-[, richland wa car dealerships, 37396, nitro remote control cars, hrdfr, jump start your car,
PP, mean car graphics, mwfo, car tires 2006 acura rl, >:PP, richland wa rent to own cars, yqqj,
The only challenge I see is the speed of the page. Does frequent use of routing slows the page loading? I am facing similar challenge in my application and avoiding routing for the fear that it may slow the page loading.
Take a look at my routes.php. I posted it at CodeIgniter forums so the code looks fine there. http://codeigniter.com/forums/viewthread/48510/#744516
I guess that’s the best solution so far.
you helped me so much! ty
Previously I was rewriting a majority of my controllers, this will keep it much more simple. Great tip!
Thanks
I was looking for a solution like this for hours, thank you so much!
You can also store routes regular expressions in your database and then override _set_routing function from the Router class in your own MY_Router class and after passing the $this->routes = $route;
You can add the ones from the database to the $this->routes property and that’s it, CI will know about the routes that could be created dynamically.
A little bit of internet surfing and observed your weblog appears to be a lttle bit screwed up in my K-meleon browser. But fortunately rarely anyone makes use of it any more however you might want to take a look into it.
Thanks For This Information. But I Need Not Show Any Controller And Function Name In My URL. I Only Want To Send Parameters Only To Different Functions. I want to Setting My Root File Which Parameter Goes To Which Function. Is It Possible Or Not.
WAQAS ALI – LAHORE – PAKISTAN.
Exterminator business is great. Always something new to see
Thanks, this is just what I was looking for!
tnx guys!
Thanks, it saves my time.
A great deal, this valuable put up is truly the nicest about this successful subject. My wife and i harmonise employing your a conclusion and can thirstily look ahead to your favorite arriving fresh news. Revealing i am grateful won’t only get an adequate amount of, to your extraordinary quality from your simply writing. I’m able to quickly receive your ultimate feed to vacation told of upgrades. Vital employment and a lot becoming successful on the home business ventures! Please justification my very own very poor Words currently never your really language.
Worked like a charm. Thanks
You actually make it seem really easy along with your presentation but I find this matter to be really something that I think I might never understand. It seems too complex and very wide for me. I’m looking forward in your next submit, I’ll attempt to get the cling of it!