我使用谷歌代码点火器,我想使用网站地图,但出现以下错误,如何修复?
我从这里开始上课:http://codeigniter.com/wiki/Google_Sitemaps
错误:
致命错误:在中找不到类"google_sitemap"D: ''axamplep''htdocs''application''controllers''sitemap_google.php,第13行
这是Controller中的完整代码:D:''examplep''htdocs''application''controllers''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( $fp=@fsockopen($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 );
}
}
$this->load->plugin('google_sitemap'); //Load Plugin
你说:
我使用Codeigniter的最后一个版本。
Codeigniter中没有更多的"插件"。
看起来在一个文件中至少需要两个类:google_sitemap_item
和google_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');
}