I am creating a site that will have a lot of routes that will need to be added to the routes file (800+) - obviously I do not want to manually add these one by one into the routes config file.
Can anyone suggest the best practice to have my routes loaded automatically from the database. Is there a suitable library/helper that will perform this task that works with version 2.x
For instance..
$route['apple'] = 'brands/index';
$route['blackberry'] = 'brands/index';
$route['htc'] = 'brands/index';
$route['samsung'] = 'brands/index';
These are just a few of the brands i'd be adding to the routes - but there are hundreds of these so i'd want to have this loaded from a database rather than manually type these in. Also - would this method have any impact on site performance having them load from the database?
I am using Codeigniter v2.1.3
If you were to constantly query the database (on each page load) via a DB call in the
application/config/routes.php
file then I imagine there would be a large impact on performance.What I would suggest (and the way I have done this previously) is to include the following line at the bottom of your
routes.php
file and save all your routes to aroutes.php
file in the cache folder.You can then write all your results to the cache file (using CI's file helper) with a function similar to the below:
If you are adding new routes in an admin area, just run the above function each time you submit your new route and it will re-generate your routes cache file.
This method keeps your routes in
application/config/routes.php
intact, but allows you to write new routes to the cache file, without drastically affecting the performance of the site.Hope that helps!!
This is an old question but i was recently faced the same dilemma and found this answer useful.
http://osvaldas.info/smart-database-driven-routing-in-codeigniter
Create a table where you put all your routes
Inside application/config/routes.php
//top two are the originals inside routes
//the new editions
example i worked with INSERT INTO
app_routes
(slug
,controller
) VALUES ('about', 'pages/about');or you can write a small cms to control your routes which is what i did as per the last bits on the website
I may have a solution to manage custom uri in a CI application.
Let say there is a table named "routes"
Then a custom "url helper" (application/helper/MY_url_helper.php)
Then a library I called 'route_manager' that creates / modifies a route and generate the custom route.php file (application/libraries/route_manager.php, creates application/cache/routes.php)
Note : Your app must be able to write files in 'application/cache'
Then add a require_once (the 4th line in the example below) in your application/config/ routes.php file
All will work using nice uri!
Exemple to create a custom fiendly uri, let's say 'super page that ROCKS! (really)' must be routed to "page" controller, "index" function with '23' as a parameter :
Example to use that custom uri
This example will generate a friendly and working url like this:
http://www.yourwebsite.com/index.php/super-page-that-rocks-really