Welcome Guest, Not a member yet? Register   Sign In
How to call controllers from a database?
#1

[eluser]Bart v B[/eluser]
Hello all,

At this moment I am making an application to CodeIgniter.
It is intended that this application based on domain names will work.
Each domain will have a database created, and become as a config file called.

Now, I want each domain based on what the controllers in the database calls.
For example, a domain want to have a forum application and a blog application.

So in the database stands:
Controller : forum
and
Controller : blog

if there is a controller called that's not in the database then cal a 404 page.

I must add that it is a CodeIgniter installation, which is outside the DOCUMENT_ROOT. I have been searching here on the forum, but this does not take me more than I own a piece of php code will have to write and this extends to the CI_ROUTER. Only how?

What is the best method?
Has anyone ever made? Or maybe someone has some inspiration for me?
Perhaps a little example?

Thanks!
#2

[eluser]Bart v B[/eluser]
I think I've found a solution.
Really dirty, but it works fine :coolsmirk:

Just make a hook..

Code:
<?php

class RouterHook
{
    function GetRoutes()
    {

       //connect to the database in my case is that also dynamic ;)
       $db_conn = mysql_connect(DB_HOST, DB_USER, DB_PW);
       mysql_select_db(DBASE,$db_conn);
    
       // whipe out the '/' from the request_uri
       $uri = str_replace('/', '', $_SERVER['REQUEST_URI'] );
        
       $sql = "SELECT controller
               FROM
               routes
               WHERE
               controller = '".mysql_real_escape_string($uri)."'";
        
       $result = mysql_query($sql);
       // count the rows
       $numRows = mysql_num_rows($result);
       // if the controller does not exists..    
       if($numRows == 0)
       {
         //but when it's not the default controller
         if($_SERVER['REQUEST_URI'] != '/')
         {
           // were going to give it an error..
           $this->error();                
         }
            
       }    
        mysql_close($db_conn);    
    }
    
  function error()
  {
    //Tada ;)
    header('Location:/error/');
    exit();
  }
}
#3

[eluser]mddd[/eluser]
Using the database for this is inefficient. Every request will cause the database to be queried for information that is not likely to change often.
You could just as easily include a php script like this:
Code:
define('CONTROLLERS', 'blog|forum');

In stead of reading and checking from the database you would then check in your hook:
Code:
if (in_array($controller, explode('|', CONTROLLERS)))
{
// go on to the controller
}
else
{
// controller not allowed -- show error
}
#4

[eluser]Bart v B[/eluser]
Hi mddd,

You're right. This would be a better solution.
Only i have the problem that I need to adjust the entire array as the domain holder has to have something.
For example:
Suppose someone has a weblog and forum application running. He or she would like to have a webshop. Then the entire array must be adjusted.

Hmm .. this is not so difficult. I'll go see if I can make some of this.

Thank you for your contribution.
#5

[eluser]pickupman[/eluser]
Use a cache library with your DB method. The calls to the DB can be cached for a set period of time. Check out Phil Sturgeon's cache library in my signature.




Theme © iAndrew 2016 - Forum software by © MyBB