提交 007d01d4 编写于 作者: R root

fecshop sitemap xml文件的生成部分

上级 2b3d7517
<?php
/**
* FecShop file.
*
* @link http://www.fecshop.com/
* @copyright Copyright (c) 2016 FecShop Software LLC
* @license http://www.fecshop.com/license/
*/
?>
<?php
return [
'sitemap' => [
'class' => '\fecshop\app\console\modules\Sitemap\Module',
],
];
<?php
/**
* FecShop file.
*
* @link http://www.fecshop.com/
* @copyright Copyright (c) 2016 FecShop Software LLC
* @license http://www.fecshop.com/license/
*/
namespace fecshop\app\console\modules\Sitemap;
use Yii;
use fecshop\app\console\modules\ConsoleModule;
/**
* @author Terry Zhao <2358269014@qq.com>
* @since 1.0
*/
class Module extends ConsoleModule
{
public $blockNamespace;
public function init()
{
# ´ָ
$nameSpace = __NAMESPACE__;
$this->controllerNamespace = $nameSpace . '\\controllers';
$this->blockNamespace = $nameSpace . '\\block';
parent::init();
}
}
<?php
/**
* FecShop file.
*
* @link http://www.fecshop.com/
* @copyright Copyright (c) 2016 FecShop Software LLC
* @license http://www.fecshop.com/license/
*/
namespace fecshop\app\console\modules\Sitemap\controllers;
use Yii;
use yii\base\InlineAction;
use yii\console\Controller;
/**
* @author Terry Zhao <2358269014@qq.com>
* @since 1.0
*/
class XmlController extends Controller
{
public function actionBegin(){
Yii::$service->sitemap->beginSiteMap();
}
# 首页
public function actionHome(){
Yii::$service->sitemap->home();
}
# 分类页面的页面总数
public function actionCategorypagecount(){
echo Yii::$service->sitemap->categorypagecount();
}
# 生成分类页面
public function actionCategory($pageNum){
Yii::$service->sitemap->category($pageNum);
}
# 产品页面的页面总数
public function actionProductpagecount(){
echo Yii::$service->sitemap->productpagecount();
}
# 生成产品页面
public function actionProduct($pageNum){
Yii::$service->sitemap->product($pageNum);
}
# cms page页面的页面总数
public function actionCmspagepagecount(){
echo Yii::$service->sitemap->cmspagepagecount();
}
# 生成cms page页面的sitemap
public function actionCmspage($pageNum){
Yii::$service->sitemap->cmspage($pageNum);
}
public function actionEnd(){
Yii::$service->sitemap->endSiteMap();
}
}
\ No newline at end of file
<?php
/**
* FecShop file.
*
* @link http://www.fecshop.com/
* @copyright Copyright (c) 2016 FecShop Software LLC
* @license http://www.fecshop.com/license/
*/
return [
'sitemap' => [
'class' => 'fecshop\services\Sitemap',
'sitemapConfig' => [
/**
* 对于下面的设置,您可能感觉很啰嗦,域名作为store的key,在store service中已经设置,
* 为什么需要在这里重新搞一套呢? 这样做是为了更加的灵活
*
*/
# appfront入口
'appfront' => [
# store的key(域名),
'fecshop.appfront.fancyecommerce.com' => [
'https' => false, # false代表使用http,true代表使用https
'sitemapDir' => '@appfront/web/sitemap.xml', # sitemap存放的地址
'showScriptName'=> true, # 是否显示index.php ,譬如http://www.fecshop.com/index.php/xxxxxx,当nginx没有设置重写,这里需要设置为true,这样url中会存在index.php,否则会404
# 这个设置对seo来说,设置为false最合适,也就是隐藏 url中index.php ,这种设置需要开启nginx的url重写
],
# store的key(域名)
'fecshop.appfront.fancyecommerce.com/fr' => [
'https' => false, # false代表使用http,true代表使用https
'sitemapDir' => '@appfront/web/fr/sitemap.xml', # sitemap存放的地址
'showScriptName'=> true,
],
'fecshop.appfront.es.fancyecommerce.com' => [
'https' => false, # false代表使用http,true代表使用https
'sitemapDir' => '@appfront/web/sitemap_es.xml',
'showScriptName'=> true,
],
'fecshop.appfront.fancyecommerce.com/cn' => [
'https' => false, # false代表使用http,true代表使用https
'sitemapDir' => '@appfront/web/sitemap_cn.xml',
'showScriptName'=> true,
],
]
],
],
];
\ No newline at end of file
<?php
/**
* FecShop file.
*
* @link http://www.fecshop.com/
* @copyright Copyright (c) 2016 FecShop Software LLC
* @license http://www.fecshop.com/license/
*/
namespace fecshop\services;
use Yii;
use yii\base\InvalidValueException;
use yii\base\InvalidConfigException;
use fec\helpers\CSession;
use fec\helpers\CUrl;
/**
* Shipping services
* @author Terry Zhao <2358269014@qq.com>
* @since 1.0
*/
class Sitemap extends Service
{
public $numPerPage = 100;
public $sitemapConfig;
protected $currentDate; // = date('Y-m-d');
protected function initSiteMap(){
$this->currentDate = date('Y-m-d');
}
protected function actionBeginSiteMap(){
$this->initSiteMap();
if(is_array($this->sitemapConfig) && !empty($this->sitemapConfig)){
foreach($this->sitemapConfig as $appIn => $store){
if(is_array($store) && !empty($store)){
foreach($store as $domain => $info){
$sitemapDir = (isset($info['sitemapDir']) && $info['sitemapDir']) ? $info['sitemapDir'] : '';
if($sitemapDir){
$sitemapAbsoluteDir = Yii::getAlias($sitemapDir);
$xmlFile = fopen($sitemapAbsoluteDir, 'w') or die("Unable to open file!");
if(file_exists($sitemapAbsoluteDir)){
$str = '<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="gss.xsl"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.google.com/schemas/sitemap/0.84 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
';
fwrite($xmlFile, $str);
}
fclose($xmlFile);
}
}
}
}
}
}
protected function actionEndSiteMap(){
echo '111'."\n";
$this->initSiteMap();
if(is_array($this->sitemapConfig) && !empty($this->sitemapConfig)){
foreach($this->sitemapConfig as $appIn => $store){
if(is_array($store) && !empty($store)){
foreach($store as $domain => $info){
if($sitemapDir){
$sitemapAbsoluteDir = Yii::getAlias($sitemapDir);
$xmlFile = fopen($sitemapAbsoluteDir, 'a') or die("Unable to open file!");
if(file_exists($sitemapAbsoluteDir)){
$str = '</urlset>';
//echo '111'."\n";
fwrite($xmlFile, $str);
}
fclose($xmlFile);
}
}
}
}
}
}
protected function actionHome(){
$this->initSiteMap();
if(is_array($this->sitemapConfig) && !empty($this->sitemapConfig)){
foreach($this->sitemapConfig as $appIn => $store){
if(is_array($store) && !empty($store)){
foreach($store as $domain => $info){
$https = $info['https'];
$showScriptName = $info['showScriptName'];
$sitemapDir = (isset($info['sitemapDir']) && $info['sitemapDir']) ? $info['sitemapDir'] : '';
if($sitemapDir){
$sitemapAbsoluteDir = Yii::getAlias($sitemapDir);
$xmlFile = fopen($sitemapAbsoluteDir, 'a') or die("Unable to open file!");
if(file_exists($sitemapAbsoluteDir)){
$home_url = Yii::$service->url->getUrlByDomain('',[],$https,$domain,$showScriptName);
$str = '<url><loc>'.$home_url.'</loc><lastmod>'.$this->currentDate.'</lastmod></url>';
fwrite($xmlFile, $str);
}
fclose($xmlFile);
}
}
}
}
}
}
protected function actionCategorypagecount(){
$this->initSiteMap();
$coll = Yii::$service->category->coll($filter);
$count = $coll['count'];
echo ceil($count/$this->numPerPage);
}
protected function actionCategory($pageNum){
$this->initSiteMap();
if(is_array($this->sitemapConfig) && !empty($this->sitemapConfig)){
foreach($this->sitemapConfig as $appIn => $store){
if(is_array($store) && !empty($store)){
foreach($store as $domain => $info){
$https = $info['https'];
$showScriptName = $info['showScriptName'];
$sitemapDir = (isset($info['sitemapDir']) && $info['sitemapDir']) ? $info['sitemapDir'] : '';
if($sitemapDir){
$sitemapAbsoluteDir = Yii::getAlias($sitemapDir);
$xmlFile = fopen($sitemapAbsoluteDir, 'a') or die("Unable to open file!");
if(file_exists($sitemapAbsoluteDir)){
$filter = [
'numPerPage' => $this->numPerPage,
'pageNum' => $pageNum,
'asArray' => true,
];
$coll = Yii::$service->category->coll($filter);
$data = $coll['coll'];
if(is_array($data) && !empty($data)){
foreach($data as $one){
$category_url_key = $one['url_key'];
$category_url = Yii::$service->url->getUrlByDomain($category_url_key,[],false,$domain,$showScriptName);
$str = '<url><loc>'.$category_url.'</loc><lastmod>'.$this->currentDate.'</lastmod></url>';
fwrite($xmlFile, $str);
}
}
}
fclose($xmlFile);
}
}
}
}
}
}
protected function actionProduct(){
}
protected function actionCmspage(){
}
}
\ No newline at end of file
......@@ -144,6 +144,42 @@ class Url extends Service
return Yii::$service->url->rewrite->getOriginUrl($urlKey);
}
/**
* @property $url_key | String urlKey的值
* @property $params | Array 。url里面个各个参数
* @property https | boolean 是否使用https的方式
* @property $domain | String , 相应的域名,譬如www.fecshop.com
* 通过传入domain的方式得到相应的url
* 该功能一般是在脚本中通过各个域名的传入得到相应的url,譬如sitemap的生成就是应用了这个方法得到
* 产品和分类的url。
*/
protected function actionGetUrlByDomain($url_key,$params=[],$https=false,$domain,$showScriptName = false){
$first_str = substr($url_key,0,1);
if($first_str == '/'){
$jg = '';
}else{
$jg = '/';
}
$baseUrl = '';
if($https){
$baseUrl = 'https://'.$domain;
}else{
$baseUrl = 'http://'.$domain;
}
if($showScriptName){
$baseUrl .= '/index.php';
}
if(is_array($params) && !empty($params)){
$arr = [];
foreach($params as $k => $v){
$arr[] = $k.'='.$v;
}
return $baseUrl.$jg.$url_key.'?'.implode('&',$arr);
}
return $url_key ? $baseUrl.$jg.$url_key : $baseUrl;
}
/**
* @property $path|String, for example about-us.html, fashion-handbag/women.html
* genarate current store url by path.
......
#!/bin/sh
Cur_Dir=$(cd `dirname $0`; pwd)
$Cur_Dir/../../../../yii sitemap/xml/begin
$Cur_Dir/../../../../yii sitemap/xml/home
categoryPageCount=`$Cur_Dir/../../../../yii sitemap/xml/categorypagecount`
echo "There are $categoryPageCount page product to process"
for (( i=1; i<=$categoryPageCount; i++ ))
do
$Cur_Dir/../../../../yii sitemap/xml/category $i
echo "Page $i done"
done
$Cur_Dir/../../../../yii sitemap/xml/end
echo 'end success'
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册