2017-01-22 10 views
2

誰かがcodeigniterを通してsitemap.xmlファイルを更新するためのcodeigniterライブラリについて考えている人はいますか?このチュートリアルに従おうとしましたが、どのファイルを作成するかわからず、どこに:https://github.com/chemicaloliver/codeigniter-sitemaps。 私は何か助けていただければ幸いです。codeigniterにsitemap.xmlファイルを更新するには?

ありがとうございました。

+3

ようCodeIgniterのでサイトマップの生成]の可能な複製をライブラリファイルのビルド機能を変更する(http://stackoverflow.com/questions/11186051/sitemap-generation-with-codeigniter) –

+0

sitemap.xmlファイルを更新するためのライブラリファイル。 –

答えて

0

ライブラリファイルをapplication/librariesフォルダにsitemapsという名前で置き、sitemaps設定ファイルをconfigフォルダに追加するだけです。 その後

   $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)); 

    } 

は、単にXMLファイルのみを保持し、それを更新するために、あなたのlibraruを変更します。だから私は使用したい

   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

ありがとうございます。その仕事は完璧です。 –

関連する問題