CodeIgniter Forums
Routing urls from database? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Routing urls from database? (/showthread.php?tid=26402)

Pages: 1 2


Routing urls from database? - El Forum - 01-12-2010

[eluser]ChiefChirpa[/eluser]
Hi,

I've been asked to overhaul an old site, I'd like to use CodeIgniter (I've used if for one small project so far) however this site has 4000+ pages all with single segment URLs (i.e domain.com/just_this_segment), obviously they don't want their URLs changed, so I am wondering if this is possible with CodeIgniter without hacking it to death?

(and just to clarify, the 4000+ URLs are not all the same functionality so would need several different controllers, so essentially I need to be able to look up in a database what content_type a url is and route it based on that (unless there is some other way of doing this))

Thanks for any advice.


Routing urls from database? - El Forum - 01-12-2010

[eluser]richthegeek[/eluser]
At the end of your routes file:
Code:
$route["(:any)"] = "router/go/$1";

In the router::go() method:
Code:
function go( $url )
{
    $this->db->where( "input_url", $url );
    $page = $this->db->get( "table" );
    if( $page->num_rows() == 0 ) show_404( $url );
    else redirect( $page->output_url );
}

That code's just a guess from your post, but it'd be a solution.


Routing urls from database? - El Forum - 01-12-2010

[eluser]Colin Williams[/eluser]
You technically can just connect the DB right there in routes, but thats messy. Going with the catchall or fallback route is certainly the way to go.


Routing urls from database? - El Forum - 01-14-2010

[eluser]scooby_dev[/eluser]
This solution doesnt work if you use modular extension... (:any) gets included in the end of the route.php file but before module routing which means (:any) overide rest of routing and route from module has not have a chance to take place.

Anyone spotted that problem using Modular Extension?


Routing urls from database? - El Forum - 01-24-2010

[eluser]ChiefChirpa[/eluser]
@richthegeek - I am afraid redirecting on every page load is simply unacceptable.

@Colin Williams - Maybe I'm missing something but if I simply send everything to a fallback route that goes to one controller, I am then stuck with one controller (from my understanding I can only load one controller in Codeigniter) which would be a real unmanageable mess.

I've looked at putting something in the Router class but it seems from another thread on this that it means you end up making two separate connections to the database (I assume because the Router is loaded before the loader class and CI super object is created?)

It looks like my only option is to instantiate some sort of front controller / bootstrap, whatever in the codeigniter.php file after the base file has been loaded and before the controller is included, unless I am missing something...


Routing urls from database? - El Forum - 01-24-2010

[eluser]richthegeek[/eluser]
ChiefChirpa - you aren't redirecting on every page load, you are just using all of the old one-segment URIs as shortcuts/aliases to the new layout. Once someone has got onto the site (bookmarks or external links) the new URI structure will be used.

Also, "simply unnaceptable" sounds kinda aggresive for a community site.. but i'm tired and full of adrenalin from the gym so sorry if it was intended as such.


Routing urls from database? - El Forum - 01-24-2010

[eluser]ChiefChirpa[/eluser]
Sorry richthegeek I didn't mean it to sound aggressive...

Your solution doesn't solve the problem (my fault I probably wasn't very clear on my initial post) they do not want new URLs, they want to use the single segment type urls they have. So I'm not looking for something to transfer them from old to new URLs, but a proper way of using DB based routing in codeigniter so they can continue to use single segment URLs.


Routing urls from database? - El Forum - 06-21-2011

[eluser]dakipro[/eluser]
Did you/anyone solved this?
I need same/similar thing, where single segment will determine controller/method/param route from the database.
For example, site will have
www.site.com/my_page.html
and that will internally be checked from the database, and then rerouted (invisibly) to the
page_controller/show_page/my_page_id
or another example
www.site.com/my_product2.html
and that will be checked in database and it will be rerouted to say:
products/show_product/my_product_id etc..
The thing is that this needs dynamic rerouting from the database, perhaps at some point before any controller is initialized at all.. not sure if this is possible, will look in core controller function if I can customize it somehow, but if someone has any ideas, please write.
Thanks guys, CI rocks! Smile


Routing urls from database? - El Forum - 06-21-2011

[eluser]hadilab[/eluser]
Quote: test



Routing urls from database? - El Forum - 06-23-2011

[eluser]stommert[/eluser]
Hi,

I use the following code to route the url using a database and a MY_Router class.
It is on combination with modular seperation.
If you want to the complete MY_Router that I use, then I will publish it somehow/somewhere



Code:
public function _validate_request($segments) {

        include(APPPATH.'config/database.php');

        $db_conn = mysql_connect($db['default']['hostname'],$db['default']['username'],$db['default']['password']);
        //check if connection is possible
        if (!$db_conn)
        {
            die('Not connected : ' . mysql_error());
        }

        mysql_select_db($db['default']['database'],$db_conn);

        $db_selected = mysql_select_db($db['default']['database'],$db_conn);

        //check if database is accesabel
        if (!$db_selected)
        {
                die ('Can\'t use '.$db['default']['hostname'].' : ' . mysql_error());
        }

            $routearray = explode('/', $_SERVER["REQUEST_URI"]);

        //create your own select query to match the external and the internal route
        $sql = "SELECT str_controller FROM tbl_routes WHERE str_external_url = '".mysql_real_escape_string($routearray[1])."' ";
        $query = mysql_query($sql,$db_conn);

        //$routes = array();

        $dbroute =  mysql_fetch_array($query, MYSQL_ASSOC);
        mysql_free_result($query);
        mysql_close($db_conn);


        if ($dbroute !== FALSE)
        {
            $segments = explode('/',$dbroute['str_controller']);
            $this->locate($segments);
        }


        /* locate module controller */
        if ($located = $this->locate($segments)) return $located;

        /* use a default 404 controller */
        if (isset($this->routes['404']) AND $segments = explode('/', $this->routes['404'])) {
            if ($located = $this->locate($segments)) return $located;
        }


        /* no controller found */
        show_404();
    }