2017-01-22 40 views
2

如果有人對codeigniter庫有關於通過codeinginter更新sitemap.xml文件的想法嗎?試圖按照本教程,但不知道要創建什麼文件,並在哪裏:https://github.com/chemicaloliver/codeigniter-sitemaps。 我會感謝任何幫助。如何在codeigniter中更新sitemap.xml文件?

謝謝。

+3

[網站地圖生成與笨(http://stackoverflow.com/questions/11186051/sitemap-generation-with-codeigniter) –

+0

的可能重複我想使用庫文件來更新sitemap.xml文件。 –

回答

0

只需將庫文件放在名爲sitemaps的application/libraries文件夾中,並在config文件夾中添加sitemaps配置文件即可。 然後在你的控制器裝載庫作爲

   $this->load->library('sitemaps'); 

,並在控制器的方法做

function add() 
    { 
     $item = array(
     "loc" => site_url("page/" . "your title of page"), 
     "lastmod" => date("c", strtotime('2017-01-01')), 
     "changefreq" => "daily", 
     "priority" => "0.8" 
    ); 

    $this->sitemaps->add_item($item); 
    // file name may change due to compression 
    $file_name = $this->sitemaps->build("sitemap.xml"); 
    $this->sitemaps->ping(site_url($file_name)); 

    } 

然後,只需修改libraru只保留XML文件,並進行更新。因此,修改庫文件的編譯功能

   function build($file_name = null, $gzip = NULL) 
{ 
    $map = $this->CI->config->item('sitemaps_header') . "\n"; 
    $xml=simplexml_load_file($file_name); 
    foreach($xml->children() as $books) { 
    $books['loc'] = htmlentities($books['loc'], ENT_QUOTES); 
     $map .= "\t<url>\n\t\t<loc>" . $books->loc . "</loc>\n"; 
     $map .= "\t\t<lastmod>" . $books->lastmod . "</lastmod>\n"; 
     $map .= "\t\t<changefreq>" . $books->changefreq . "</changefreq>\n"; 
     $map .= "\t\t<priority>" . $books->priority . "</priority>\n"; 
     $map .= "\t</url>\n\n"; 
     } 


    foreach ($this->items as $item) 
    { 
     $item['loc'] = htmlentities($item['loc'], ENT_QUOTES); 
     $map .= "\t<url>\n\t\t<loc>" . $item['loc'] . "</loc>\n"; 
     $attributes = array("lastmod", "changefreq", "priority"); 
     foreach ($attributes AS $attr) 
     { 
      if (isset($item[$attr])) 
      { 
       $map .= "\t\t<$attr>" . $item[$attr] . "</$attr>\n"; 
      } 
     } 

     $map .= "\t</url>\n\n"; 
    } 
    //unset($this->items); 
    $map .= $this->CI->config->item('sitemaps_footer'); 
    if (!is_null($file_name)) 
    { 
     $fh = fopen($file_name, 'w'); 
     if (!$fh) 
     { 
      $this->set_error('Could not open sitemaps file for writing: ' . $file_name); 
      return FALSE; 
     } 
     if (!fwrite($fh, $map)) 
     { 
      $this->set_error('Error writing to sitemaps file: ' . $file_name); 
      return FALSE; 
     } 
     fclose($fh); 
     if ($this->CI->config->item('sitemaps_filesize_error') && filesize($file_name) > 1024 * 1024 * 10) 
     { 
      $this->set_error('Your sitemap is bigger than 10MB, most search engines will not accept it.'); 
      return FALSE; 
     } 
     return $file_name; 
    } 
    else 
    { 
     return $map; 
    } 
} 
+0

謝謝先生。它的工作完美。 –

相關問題