[yiic] Sitemap Generation

Something like this: http://www.xml-sitemaps.com/

might even be able to use yii webservices to just use that website to generate them. i'll try and take a stab at something like this tomorrow.

after you pass in a url it gives you your sitemaps back:

http://www.xml-sitem…Yii/sitemap.xml

sample generation:

  <?xml version="1.0" encoding="UTF-8" ?> 


- <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">


- <!--  created with Free Online Sitemap Generator www.xml-sitemaps.com 


  --> 


- <url>


  <loc>http://valhalla-studios.com/VStudios_Yii/</loc> 


  <priority>1.00</priority> 


  <changefreq>daily</changefreq> 


  </url>


- <url>


  <loc>http://valhalla-studios.com/VStudios_Yii/home.html</loc> 


  <priority>0.80</priority> 


  <changefreq>daily</changefreq> 


  </url>


- <url>


  <loc>http://valhalla-studios.com/VStudios_Yii/ourWork.html</loc> 


  <priority>0.80</priority> 


  <changefreq>daily</changefreq> 


  </url>


- <url>


  <loc>http://valhalla-studios.com/VStudios_Yii/portfolio.html</loc> 


  <priority>0.80</priority> 


  <changefreq>daily</changefreq> 


  </url>


- <url>


  <loc>http://valhalla-studios.com/VStudios_Yii/FAQ.html</loc> 


  <priority>0.80</priority> 


  <changefreq>daily</changefreq> 


  </url>


- <url>


  <loc>http://valhalla-studios.com/VStudios_Yii/priceQuote.html</loc> 


  <priority>0.80</priority> 


  <changefreq>daily</changefreq> 


  </url>


- <url>


  <loc>http://valhalla-studios.com/VStudios_Yii/contactUs.html</loc> 


  <priority>0.80</priority> 


  <changefreq>daily</changefreq> 


  </url>


  </urlset>

This is more appropriate to be an extension. There are many tools that can generate sitemap.

Ok, sure; and yes, there is.

I’ll see if I can’t make a cool yii command  ;)

I made php sitemap generator for yii, and it's very slow. I think the best way to go about this is to create a java app that can be triggered by a yii command.

Yes, it could be an extension in terms of a yiic command. Since it is running offline, the performance is not of top concern.

Sitemap is easy enough.

In url rules:


            	'sitemap.xml'=>'site/sitemapxml',

In site controller:


	public function actionSitemapxml()

	{


    	$posts=Post::model()->findAll(array(

        	'order'=>'create_time DESC',

        	'condition'=>'status="swPost/published"',

    	));


    	$pages=Page::model()->findAll(array(

        	'order'=>'create_time DESC',

        	'condition'=>'status="swPost/published"',

    	));


    	header('Content-Type: application/xml');

    	$this->renderPartial('../site/sitemapxml',array('posts'=>$posts,'pages'=>$pages));

	}



views/site/sitemapxml:


<?php echo '<?xml version="1.0" encoding="UTF-8"?>' ?>


<urlset

  	xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"

  	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  	xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9

        	http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">


<?php foreach($posts as $model): ?>

	<url>

    	<loc><?php echo CHtml::encode($this->createAbsoluteUrl('post/view',array('slug'=>$model->slug))); ?></loc>

    	<changefreq>weekly</changefreq>

    	<priority>0.5</priority>

	</url>

<?php endforeach; ?>


<?php foreach($pages as $model): ?>

	<url>

    	<loc><?php echo CHtml::encode($this->createAbsoluteUrl('page/view',array('slug'=>$model->slug))); ?></loc>

    	<changefreq>weekly</changefreq>

    	<priority>0.5</priority>

	</url>

<?php endforeach; ?>


</urlset>



It could probably use caching, but it’s fairly fast.

I was looking for a quick extension to do this, but thanks to your post Jacmoe I was able to get this task done very quickly.

Rather than specify the criteria for the sitemap in the controller, I added a scope to each model which is the ‘sitemap’ scope, setting the select etc. criteria I want each item to be sorted by, then built one primary list in the controller containing the formatted URLs and custom frequency and priority per item, which let me make the sitemapxml file more direct and simply loop through the list, like so:

model scopes




	/**

	 * Preset scope conditions

	 * @return array[]

	 */

	public function scopes()

	{

		return array(

			'sitemap'=>array('select'=>'Id', 'condition'=>'startDate <= NOW()', 'order'=>'Id ASC'),

		);

	}



SiteController




	/**

	 * Render the sitemap as human readable text

	 */

	public function actionSitemap()

	{

		$list = array();

		

		$this->populateSitemap($list);


        	$this->render('sitemap',array('list'=>$list));

	}

	/**

	 * Render the sitemap in XML form

	 */

	public function actionSitemapXML()

	{

		$list = array();

		

		$this->populateSitemap($list);

		

       		header('Content-Type: application/xml');

        	$this->renderPartial('sitemapxml',array('list'=>$list));

	}	

	/**

	 * Populate the array of site links

	 * @param array[] &$list The array which holds the array of link information: loc, frequency, priority

	 */

	public function populateSitemap( &$list )

	{


		$prizes = Prize::model()->sitemap()->findAll();

		$tasks = Task::model()->sitemap()->findAll();

		

		// Add primary items here		

		$list[] = array(

				'loc'=>$this->createAbsoluteUrl('/'),

				'frequency'=>'weekly',

				'priority'=>'1',

				);

		$list[] = array(

				'loc'=>$this->createAbsoluteUrl('/site/contact'),

				'frequency'=>'yearly',

				'priority'=>'0.8',

				);

		$list[] = array(

				'loc'=>$this->createAbsoluteUrl('/site/page', array('view'=>'about')),

				'frequency'=>'monthly',

				'priority'=>'0.8',

				);

		$list[] = array(

				'loc'=>$this->createAbsoluteUrl('/site/page', array('view'=>'privacy')),

				'frequency'=>'yearly',

				'priority'=>'0.3',

				);			

		

		foreach( $prizes as $row )

		{

			$list[] = array(

					'loc'=>	$this->createAbsoluteUrl('/drawing/view',array('id'=>$row->Id )),

					'frequency'=>'weekly',

					'priority'=>'0.5',

			);

		}

		foreach( $tasks as $row )

		{

			$list[] = array(

					'loc'=>	$this->createAbsoluteUrl('/activities/view',array('id'=>$row->Id )),

					'frequency'=>'weekly',

					'priority'=>'0.5',

			);

		}		

	}



sitemapxml.php view




<?php echo '<?xml version="1.0" encoding="UTF-8"?>' ?>


<urlset

        xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"

        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

        xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9

                http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">


<?php foreach($list as $row): ?>

        <url>

        <loc><?php echo CHtml::encode($row['loc']); ?></loc>

        <changefreq><?php echo $row['frequency']?></changefreq>

        <priority><?php echo $row['priority'];?></priority>

        </url>

<?php endforeach; ?>


</urlset>

I feel like with a little more focused attention it could easily be abstracted into an extension that would allow to pre-set some basic site pages and a list of models / configuration options.