2010-03-24 59 views
2

我无法将模型加载到codeigniter中的扩展My_Router类。以下是我的代码:如何在codeigniter中的扩展MY_Router类中加载模型

class MY_Router extends CI_Router { 

    function MY_Router() 
    { 
     parent::CI_Router(); 
    } 

    function _validate_request($segments) 
    { 
     // Does the requested controller exist in the root folder? 
     if (file_exists(APPPATH.'controllers/'.$segments[0].EXT)) 
     { 
      return $segments; 
     } 

     // Is the controller in a sub-folder? 
     if (is_dir(APPPATH.'controllers/'.$segments[0])) 
     { 
      // Set the directory and remove it from the segment array 
      $this->set_directory($segments[0]); 
      $segments = array_slice($segments, 1); 

      if (count($segments) > 0) 
      { 
       // Does the requested controller exist in the sub-folder? 
       if (! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$segments[0].EXT)) 
       { 
        show_404($this->fetch_directory().$segments[0]); 
       } 
      } 
      else 
      { 
       $this->set_class($this->default_controller); 
       $this->set_method('index'); 

       // Does the default controller exist in the sub-folder? 
       if (! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$this->default_controller.EXT)) 
       { 
        $this->directory = ''; 
        return array(); 
       } 

      } 
      return $segments; 
     } 

     // Let's check if there are category segments 
    $category_routes = $this->category_routing($segments); 
     if($category_routes !== FALSE) 
    { 
      return $category_routes; 
     } 
    $user_routes = $this->user_routing($segments); 
    if($user_routes != FALSE) 
    { 
     return $user_routes; 
    } 

     show_404($segments[0]); 
    } 

    function category_routing($segments) 
    { 
     $this->load->model('category_model'); 
     if($this->category_model->category_exist($segments[0])) 
     { 
      //if only category 
      if(count($segments)==1) 
      { 
       return array('category', 'category_browse', $segments[0]); 
      } 
      //category pagination 
      if(count($segments)==2 and is_numeric($segments[1])) 
      { 
       return array('category','category_browse', $segments[0], $segments[1]); 
      } 
      //category upcoming 
      if(count($segments)==2 and $segments[1] == 'upcoming') 
      { 
       return array('category','upcoming', $segments[0]); 
      } 
      //category upcoming pagination 
      if(count($segments)==3 and $segments[1] == 'upcoming' and is_numeric($segments[3])) 
      { 
       return array('category','upcoming', $segments[0], $segments[3]);  
      } 
      //category top 
      if(count($segments)==3 and $segments[1] == 'top') 
      { 
       return array('category','top', $segments[0], $segments[2]); 
      } 
      //category top pagination 
      if(count($segments)==4 and $segments[1] == 'top' and is_numeric($segments[3])) 
      { 
       return array('category','top', $segments[0], $segments[3]); 
      } 
     } 
     return FALSE; 
    } 

    function user_routing($segments) 
    { 
     $this->load->model('dx_auth/users', 'user_model'); 
     if($this->user_model->check_username($segments[0])) 
     { 
      //only profile 
      if(count($segments)==1) 
      { 
       return array('user','profile',$segments[0]);  
      } 
      //all friends 
      if(count($segments)==2 and $segment[1]=='allfriends') 
      { 
       return array('user','allfriends',$segments[0]); 
      } 
      //all subscribers 
      if(count($segments)==2 and $segment[1]=='allsubscribers') 
      { 
       return array('user','allsubscribers',$segments[0]); 
      } 
      //all subscription 
      if(count($segments)==2 and $segment[1]=='allsubscriptions') 
      { 
       return array('user','allsubscriptions',$segments[0]); 
      } 
     } 
     return FALSE; 
    } 
} 

我已经尝试使用codeigniter提供的get_instance函数加载模型,但似乎它不工作。我需要的只是在扩展系统库中加载模型。

回答

0

这是我做的,它的工作..谢谢菲尔的建议。

class MY_Router extends CI_Router { 

function MY_Router() 
{ 
    parent::CI_Router(); 
} 

function _validate_request($segments) 
{ 
    // Does the requested controller exist in the root folder? 
    if (file_exists(APPPATH.'controllers/'.$segments[0].EXT)) 
    { 
     return $segments; 
    } 

    // Is the controller in a sub-folder? 
    if (is_dir(APPPATH.'controllers/'.$segments[0])) 
    { 
     // Set the directory and remove it from the segment array 
     $this->set_directory($segments[0]); 
     $segments = array_slice($segments, 1); 

     if (count($segments) > 0) 
     { 
      // Does the requested controller exist in the sub-folder? 
      if (! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$segments[0].EXT)) 
      { 
       show_404($this->fetch_directory().$segments[0]); 
      } 
     } 
     else 
     { 
      $this->set_class($this->default_controller); 
      $this->set_method('index'); 

      // Does the default controller exist in the sub-folder? 
      if (! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$this->default_controller.EXT)) 
      { 
       $this->directory = ''; 
       return array(); 
      } 

     } 
     return $segments; 
    } 

    // Let's check if there are category segments 
    $category_routes = $this->category_routing($segments); 
    if($category_routes !== FALSE) 
    { 
     return $category_routes; 
    } 
    $user_routes = $this->user_routing($segments); 
    if($user_routes !== FALSE) 
    { 
     return $user_routes; 
    } 

    show_404($segments[0]); 
} 

function category_routing($segments) 
{ 
    if($this->check_category_exist($segments[0])) 
    { 
     //if only category 
     if(count($segments)==1) 
     { 
      return array('category', 'category_browse', $segments[0]); 
     } 
     //category pagination 
     if(count($segments)==2 and is_numeric($segments[1])) 
     { 
      return array('category','category_browse', $segments[0], $segments[1]); 
     } 
     //category upcoming 
     if(count($segments)==2 and $segments[1] == 'upcoming') 
     { 
      return array('category','upcoming', $segments[0]); 
     } 
     //category upcoming pagination 
     if(count($segments)==3 and $segments[1] == 'upcoming' and is_numeric($segments[3])) 
     { 
      return array('category','upcoming', $segments[0], $segments[3]);  
     } 
     //category top 
     if(count($segments)==3 and $segments[1] == 'top') 
     { 
      return array('category','top', $segments[0], $segments[2]); 
     } 
     //category top pagination 
     if(count($segments)==4 and $segments[1] == 'top' and is_numeric($segments[3])) 
     { 
      return array('category','top', $segments[0], $segments[3]); 
     } 
    } 
    return FALSE; 
} 

function check_category_exist($cat_name) 
{ 
    //connect to database and find the category 
    include(APPPATH.'config/database'.EXT); 
    $conn = mysql_connect($db['default']['hostname'],$db['default']['username'],$db['default']['password']); 
    mysql_select_db($db['default']['database'],$conn); 
    $sql = sprintf("SELECT COUNT(id) as count FROM categories WHERE permalink = '%s'", mysql_real_escape_string($cat_name)); 
    $query = mysql_query($sql); 
    $row = mysql_fetch_object($query); 
    mysql_close($conn); 
    if($row->count) 
    { 
     return TRUE; 
    } 
    return FALSE; 
} 

function user_routing($segments) 
{ 
    if($this->check_username_exist($segments[0])) 
    { 
     //only profile 
     if(count($segments)==1) 
     { 
      return array('user','profile',$segments[0]);  
     } 
     //all friends 
     if(count($segments)==2 and $segments[1]=='allfriends') 
     { 
      return array('user','allfriends',$segments[0]); 
     } 
     //all subscribers 
     if(count($segments)==2 and $segments[1]=='allsubscribers') 
     { 
      return array('user','allsubscribers',$segments[0]); 
     } 
     //all subscription 
     if(count($segments)==2 and $segments[1]=='allsubscriptions') 
     { 
      return array('user','allsubscriptions',$segments[0]); 
     } 
    } 
    return FALSE; 
} 

function check_username_exist($username) 
{ 
    //connect to database and find the category 
    include(APPPATH.'config/database'.EXT); 

    $conn = mysql_connect($db['default']['hostname'], $db['default']['username'], $db['default']['password']); 
    mysql_select_db($db['default']['database'],$conn); 
    $sql = sprintf("SELECT COUNT(id) as count FROM users WHERE username = '%s'", mysql_real_escape_string($username)); 
    $query = mysql_query($sql); 
    $row = mysql_fetch_object($query); 
    mysql_close($conn); 
    if($row->count) 
    { 
     return TRUE; 
    } 
    return FALSE; 
} 

}

-1

当使用外部库基笨类,你必须再次调用它是这样的:

// load it 
$CI =& get_instance(); 
$CI->load->model('model_name'); 

//use it 
$CI->model_name->method() 

希望帮助

+0

他正在路由器中使用get_instance(),这会导致致命错误。 – 2010-03-25 10:37:09

+0

是的菲尔是正确的我找到了一条出路,我做了包括数据库配置和连接到我的数据库使用PHP myslq函数,并可以访问数据库。我在这里发布我的代码。可能对尝试这样做的人有所帮助。 – Yalamber 2010-03-27 04:36:28

3

不存在对CodeIgniter的超级全局没有访问,直到CI_Base一直这称为由Controller扩展的。控制器类然后加载装载机库:

// In PHP 5 the Loader class is run as a discreet 
    // class. In PHP 4 it extends the Controller 
    if (floor(phpversion()) >= 5) 
    { 
     $this->load =& load_class('Loader'); 
     $this->load->_ci_autoloader(); 
    } 

路由器装载非常上(在系统/笨/ CodeIgniter.php一看,看什么时候,上线99),因此具有可几乎任何东西。

你可以使用load_class('Whatever');以不同的顺序加载类,但如果你不小心,这可能会带来一些问题,并且你仍然无法访问数据库驱动程序。

基本上,你不能这样做。您需要尝试直接使用数据库库或使用本机MySQL绑定来访问您的数据。

+0

感谢菲尔在下面看到我的答案,它的工作方式。 – Yalamber 2010-03-27 04:38:48

+0

使用本机MySQL绑定是我的建议。你欠我一分;-) – 2010-03-28 10:46:53

0

下面的代码将解决你的问题太多,将让您的编码极大的方便,灵活。

require_once(BASEPATH . 'database/DB' . EXT); 
$db = & DB(); 
$query = $db->query("select ..."); 
$results = $query->result();