2012-03-09 102 views
0

我使用谷歌codeigniter,我想使用站点地图,但得到以下错误,如何解决它?致命错误:找不到类'google_sitemap'

我得到这个班从这里:http://codeigniter.com/wiki/Google_Sitemaps

错误:

Fatal error: Class 'google_sitemap' not found in D:\xampp\htdocs\application\controllers\sitemap_google.php on line 13

这是控制器完整的代码在这里:d:\ XAMPP \ htdocs中\程序\控制器\ sitemap_google.php:

<?php 
class Sitemap_google extends CI_Controller 
{ 
    function My_controller() 
    { 
     parent::Controller(); 
     $this->load->helper(array('text','url')); 
     $this->load->plugin('google_sitemap'); //Load Plugin 
    } 

    function index() 
    { 
     $sitemap = new google_sitemap; //This is line 13 
     $item = new google_sitemap_item(base_url()."MY_WEBSITE_URL",date("Y-m-d"), 'weekly', '0.8'); //Create a new Item 
     $sitemap->add_item($item); //Append the item to the sitemap object 
     $sitemap->build("./sitemap.xml"); //Build it... 

     //Let's compress it to gz 
     $data = implode("", file("./sitemap.xml")); 
     $gzdata = gzencode($data, 9); 
     $fp = fopen("./sitemap.xml.gz", "w"); 
     fwrite($fp, $gzdata); 
     fclose($fp); 

     //Let's Ping google 
     $this->_pingGoogleSitemaps(base_url()."/sitemap.xml.gz"); 
    } 

    function _pingGoogleSitemaps($url_xml) 
    { 
     $status = 0; 
     $google = 'www.google.com'; 
     if([email protected]($google, 80)) 
     { 
      $req = 'GET /webmasters/sitemaps/ping?sitemap=' . 
        urlencode($url_xml) . " HTTP/1.1\r\n" . 
        "Host: $google\r\n" . 
        "User-Agent: Mozilla/5.0 (compatible; " . 
        PHP_OS . ") PHP/" . PHP_VERSION . "\r\n" . 
        "Connection: Close\r\n\r\n"; 
      fwrite($fp, $req); 
      while(!feof($fp)) 
      { 
      if(@preg_match('~^HTTP/\d\.\d (\d+)~i', fgets($fp, 128), $m)) 
      { 
       $status = intval($m[1]); 
       break; 
      } 
      } 
      fclose($fp); 
     } 
     return($status); 
    } 

} 
+0

哪个笨的版本您使用的?你可以用'echo CI_VERSION'来检查。我问,因为“插件”已被删除了一段时间。 – 2012-03-09 18:01:10

+0

我使用Codeigniter的最新版本。 – 2012-03-09 18:01:59

+0

如果'My_controller'应该是构造函数,那么你做错了。我认为你已经混淆了库和你的正常控制器。调用'__construct()'而不是'MY_Controller()'。 – JohnP 2012-03-09 18:04:17

回答

0
$this->load->plugin('google_sitemap'); //Load Plugin 

你说:

I use last version of Codeigniter.

Codeigniter中没有更多的“插件”。

它看起来像你期望在一个文件中至少有两个类:google_sitemap_itemgoogle_sitemap。 CI的装载不会与发挥好(预计每个文件一个类),所以不要用CI装载机打扰,只是直包括:

include APPPATH.'path/to/file/google_sitemap.php'); 

您还在使用旧PHP4构造函数,这表明您使用的是旧版本的CI(当前为2.1.0,您可以使用echo CI_VERSION;进行检查)。所以,这样的:

function My_controller() 
{ 
    parent::Controller(); 
    $this->load->helper(array('text','url')); 
    $this->load->plugin('google_sitemap'); //Load Plugin 
} 

应该是这样的:

function __construct() 
{ 
    parent::__construct(); 
    $this->load->helper(array('text','url')); 
    include APPPATH.'path/to/file/google_sitemap.php'); 
} 
+0

我尝试了一下,出现错误,请参阅我的网址:http://www.neginph.com/sitemap_google/ – 2012-03-09 18:39:01

+0

您的路径到'。/ sitemap.xml'不正确。请记住,CI始终在'index.php'上运行,因此所有路径都应该是相对的。该文件不存在于您的根目录中,或者您提供了错误的路径。如果失败了,请尝试使用绝对路径,例如'FCPATH.'sitemap.xml''。至少,您的类文件现在正在正确加载。 – 2012-03-09 18:43:23