1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* DefaultController for sitemap module |
4
|
|
|
* |
5
|
|
|
* @link https://github.com/himiklab/yii2-sitemap-module |
6
|
|
|
* @author Serge Larin <[email protected]> |
7
|
|
|
* @author HimikLab |
8
|
|
|
* @copyright 2015 Assayer Pro Company |
9
|
|
|
* @copyright Copyright (c) 2014 HimikLab |
10
|
|
|
* @license http://opensource.org/licenses/MIT MIT |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace assayerpro\sitemap\controllers; |
14
|
|
|
|
15
|
|
|
use Yii; |
16
|
|
|
use yii\web\Controller; |
17
|
|
|
use yii\web\NotFoundHttpException; |
18
|
|
|
use yii\web\Response; |
19
|
|
|
use assayerpro\sitemap\RobotsTxt; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* DefaultController for sitemap module |
23
|
|
|
* |
24
|
|
|
* @author Serge Larin <[email protected]> |
25
|
|
|
* @author HimikLab |
26
|
|
|
* @package assayerpro\sitemap |
27
|
|
|
*/ |
28
|
|
|
class DefaultController extends Controller |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @inheritdoc |
32
|
|
|
*/ |
33
|
|
|
public function behaviors() |
34
|
|
|
{ |
35
|
|
|
return [ |
36
|
|
|
'pageCache' => [ |
37
|
|
|
'class' => 'yii\filters\PageCache', |
38
|
|
|
'only' => ['index', 'robots-txt'], |
39
|
|
|
'duration' => Yii::$app->sitemap->cacheExpire, |
40
|
|
|
'variations' => [Yii::$app->request->get('id')], |
41
|
|
|
], |
42
|
|
|
]; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Action for sitemap/default/index |
47
|
|
|
* |
48
|
|
|
* @access public |
49
|
|
|
* @return string |
50
|
|
|
*/ |
51
|
|
|
public function actionIndex($id = 0) |
52
|
|
|
{ |
53
|
|
|
$sitemap = Yii::$app->sitemap->render(); |
54
|
|
|
if (empty($sitemap[$id])) { |
55
|
|
|
throw new NotFoundHttpException(Yii::t('yii', 'Page not found.')); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
Yii::$app->response->format = Response::FORMAT_RAW; |
59
|
|
|
$headers = Yii::$app->response->headers; |
60
|
|
|
$headers->add('Content-Type', 'application/xml'); |
61
|
|
|
$result = $sitemap[$id]['xml']; |
62
|
|
|
if (Yii::$app->sitemap->enableGzip) { |
63
|
|
|
$result = gzencode($result); |
64
|
|
|
$headers->add('Content-Encoding', 'gzip'); |
65
|
|
|
$headers->add('Content-Length', strlen($result)); |
66
|
|
|
} |
67
|
|
|
return $result; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Action for sitemap/default/robot-txt |
72
|
|
|
* |
73
|
|
|
* @access public |
74
|
|
|
* @return string |
75
|
|
|
*/ |
76
|
|
|
public function actionRobotsTxt() |
77
|
|
|
{ |
78
|
|
|
$robotsTxt = empty(Yii::$app->components['robotsTxt']) ? new RobotsTxt() : Yii::$app->robotsTxt; |
79
|
|
|
$robotsTxt->sitemap = Yii::$app->urlManager->createAbsoluteUrl( |
80
|
|
|
empty($robotsTxt->sitemap) ? [$this->module->id.'/'.$this->id.'/index'] : $robotsTxt->sitemap |
81
|
|
|
); |
82
|
|
|
Yii::$app->response->format = Response::FORMAT_RAW; |
83
|
|
|
Yii::$app->response->headers->add('Content-Type', 'text/plain'); |
84
|
|
|
return $robotsTxt->render(); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|