Yii Framework Forum: [yiic] Sitemap Generation - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

[yiic] Sitemap Generation Rate Topic: -----

#1 User is offline   DarkNSF 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 287
  • Joined: 12-November 08
  • Location:Palm Bay, Florida

Posted 31 January 2009 - 04:16 AM

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>

0

#2 User is offline   qiang 

  • Yii Project Lead
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,902
  • Joined: 04-October 08
  • Location:DC, USA

Posted 31 January 2009 - 07:31 AM

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

#3 User is offline   DarkNSF 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 287
  • Joined: 12-November 08
  • Location:Palm Bay, Florida

Posted 31 January 2009 - 02:16 PM

Ok, sure; and yes, there is.

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

#4 User is offline   DarkNSF 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 287
  • Joined: 12-November 08
  • Location:Palm Bay, Florida

Posted 01 February 2009 - 03:36 PM

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.
0

#5 User is offline   qiang 

  • Yii Project Lead
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,902
  • Joined: 04-October 08
  • Location:DC, USA

Posted 01 February 2009 - 09:20 PM

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

#6 User is offline   jacmoe 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 2,601
  • Joined: 10-October 10
  • Location:Denmark

Posted 10 December 2010 - 09:20 AM

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.
"Less noise - more signal"
4

#7 User is offline   Dana 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 203
  • Joined: 18-February 10
  • Location:Florida, USA

Posted 09 March 2011 - 08:48 AM

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.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users