Database driven URL routing.

#1
[eluser]FutureKing[/eluser]
In codeigniter when we have to do URL routing we have to modify routes.php file.I want something like this:
All the url routing config stored in db and loaded on the fly.
When user want to modify the routes he only has to use web based control panel(control panel of web app).

I want URL routing easier for my users. Therefore I want database driven URL routing.

Please guide me.

Thanks

#2
[eluser]garymardell[/eluser]
you could always generate and write to the routes file, that would be the easiest. It would also be the fastest. I know it has been done but you have more problems caused by the database not being loaded when the routing happens.

#3
[eluser]brianw1975[/eluser]
I agree, at least with reading/updating the routes files you could still use CI to do the work, and if you were hesitant to replace the actual CI file you could place a require_once() at the end of it and point that at a separate file that you would do your updates to.

If you go that route I would write the new routes to a new file and then in one fell swoop copy the new file over the old file so that ( as / if )the routes.php file grows to a significant size there won't be any 404 errors resulting from the file being inaccessible because it is being written to - at least, i think that might be an issue... i guess it would depend on the server and the other processes going on.

#4
[eluser]devbro[/eluser]
look up _remap() in the controller

If you can hold on to the name of controller or can keep some sort of pattern in the url then _remap() can take care of the rest. you just need to write some code for it.

#5
[eluser]n0xie[/eluser]
Or just extend the Router class and you have your database driven routes.

The problem with writing a routing file is that CI uses a 'first match, first serve' logic, which could give you unsuspected behaviour. I say unsuspected, because it will not be a bug: the CI routing class will do exactly as you tell it, but it might not do it in the order you want it to. Unless you keep track of what routing rule should fire first, this can get real messy, and very hard to debug.

A basic example:
Code:
$route['product/:any'] = "catalog/product_overview";
$route['product/(:num)'] = "catalog/product_lookup_by_id/$1";

vs

Code:
$route['product/(:num)'] = "catalog/product_lookup_by_id/$1";
$route['product/:any'] = "catalog/product_overview";

Even though this example is crude and very simple, you can see that the order in which you will write your rules to a file is very important. So if you do decide to go this way, you need to have a system which checks for these sort of things.

#6
[eluser]Aken[/eluser]
The way I did it was to set up a default route that directed any URLs from a specific directory into a single controller, and that controller checks the URI supplied, looks to see if it exists in the database, loads the information if it exists or sets a 404 error if not.

This is pretty easy if you set up some specific controllers for different sections. Also a single big catch-all route isn't really recommended as it'll slow things down pretty badly in a few given situations.

All-in-all it's not hard. And no need to write to a route file or anything.

#7
[eluser]Jay Logan[/eluser]
I set up a default route that points (:any) to this controller:

Code:
function index()
    {

        if ($this->site->uri_string()) {
        
            $uri_string = $this->site->uri_string();
            
        } else {
        
            $uri_string = "home";
            
        }

        $page_contents = $this->sites_model->get_page_contents(array('uri_string' => $uri_string));
        
        if ($page_contents) {
    
            $this->site->group_view($page_contents['view_file_name']);
            
        } else {
        
            show_404('page');
            
        }
    }

And I also created a Site library that does a lot of the work.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.