0

虽然我可以暂时看到一个解决方案,但我想知道是否有一个明显更简单的方法。

我的目标是使用给定 URI 的第一段来查询数据库应该运行哪个控制器。

我假设我必须使用段 1 中的结果控制器名称来修改 URI,然后允许系统继续正常处理(因此是 pre_system 挂钩)。

虽然不是必需的,但我还想从同一个 DB 请求中保留几个其他变量,以便稍后在调用堆栈中使用,并假设这必须使用全局变量来完成?

任何更好的建议都会很高兴收到。

谢谢。

4

2 回答 2

4

如果它对其他人有用,这里是实现所需结果的代码。然而,这并没有考虑到传递额外的变量,因为我可以没有它们。

function set_controller()
{
    include_once APPPATH.'config/database.php'; //Gather the DB connection settings
    $link = mysql_connect($db[$active_group]['hostname'], $db[$active_group]['username'], $db[$active_group]['password']) or die('Could not connect to server.' ); //Connect to the DB server
    mysql_select_db($db[$active_group]['database'], $link) or die('Could not select database.'); //Select the DB
    $URI = explode('/',key($_GET)); //Break apart the URL variable
    $query = 'SELECT * FROM theDomainTable WHERE domainName = "'.$URI[1].'"'; //Query the DB with the URI segment
    if($results = mysql_fetch_array(mysql_query($query))){ //Only deal with controller requests that exist in the database
        $URI[1] = $results['controllerName']; //Replace the controller segment
        $_GET = array(implode('/',$URI)=>NULL); //Reconstruct and replace the GET variable
    }
    mysql_close($link); //Close the DB link
}
于 2010-05-28T07:24:01.630 回答
0

我不会使用全局变量,如果可能的话,我更愿意将它存储在库中以便以后检索。在 CI 的上下文中,全局变量有点混乱。

尽管pre_system 此时仅加载了基准和钩子类。这意味着你几乎被全局变量卡住了,除非你能找到一种方法来选择控制器,pre_controller因为所有基类都已加载,并且你可以将数据放在更合乎逻辑的地方。

于 2010-05-21T22:02:46.353 回答