1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace luya\web; |
4
|
|
|
|
5
|
|
|
use Yii; |
6
|
|
|
|
7
|
|
|
use luya\Exception; |
8
|
|
|
use luya\helpers\StringHelper; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* LUYA web view wrapper. |
12
|
|
|
* |
13
|
|
|
* Implements additional helper methods to the Yii web controller. |
14
|
|
|
* |
15
|
|
|
* @property string $publicHtml Return the relativ path to your public_html folder |
16
|
|
|
* |
17
|
|
|
* @author Basil Suter <[email protected]> |
18
|
|
|
* @since 1.0.0 |
19
|
|
|
*/ |
20
|
|
|
class View extends \yii\web\View |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var boolean If csrf validation is enabled in the request component, and autoRegisterCsrf is enabled, then |
24
|
|
|
* all the meta informations will be auto added to meta tags. |
25
|
|
|
*/ |
26
|
|
|
public $autoRegisterCsrf = true; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Init view object. Implements auto register csrf meta tokens. |
30
|
|
|
* @see \yii\base\View::init() |
31
|
|
|
*/ |
32
|
|
|
public function init() |
33
|
|
|
{ |
34
|
|
|
// call parent initializer |
35
|
|
|
parent::init(); |
36
|
|
|
|
37
|
|
|
if (empty($this->theme) && Yii::$app->themeManager->hasActiveTheme) { |
38
|
|
|
$this->theme = Yii::$app->themeManager->activeTheme; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
// auto register csrf tags if enabled |
42
|
|
|
if ($this->autoRegisterCsrf && Yii::$app->request->enableCsrfValidation) { |
43
|
|
|
$this->registerCsrfMetaTags(); |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Get the url source for an asset. |
49
|
|
|
* |
50
|
|
|
* When registering an asset `\app\assets\ResoucesAsset::register($this)` the $assetName |
51
|
|
|
* is `app\assets\ResourcesAsset`. |
52
|
|
|
* |
53
|
|
|
* @param string $assetName The class name of the asset bundle (without the leading backslash) |
54
|
|
|
* @return string The internal base path to the asset file. |
55
|
|
|
* @throws Exception |
56
|
|
|
*/ |
57
|
|
|
public function getAssetUrl($assetName) |
58
|
|
|
{ |
59
|
|
|
$assetName = ltrim($assetName, '\\'); |
60
|
|
|
|
61
|
|
|
if (!isset($this->assetBundles[$assetName])) { |
62
|
|
|
throw new Exception("The AssetBundle '$assetName' is not registered."); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return $this->assetBundles[$assetName]->baseUrl; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Removes redundant whitespaces (>1) and new lines (>1). |
70
|
|
|
* |
71
|
|
|
* @param string $content input string |
72
|
|
|
* @return string compressed string |
73
|
|
|
*/ |
74
|
|
|
public function compress($content) |
75
|
|
|
{ |
76
|
|
|
return StringHelper::minify($content); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Return the relativ path to your public_html folder. |
81
|
|
|
* |
82
|
|
|
* This wrapper function is commonly used to get the path for images or other files inside your |
83
|
|
|
* public_html directory. For instance you have put some images in our public folder `public_html/img/luya.png` |
84
|
|
|
* then you can access the image file inside your view files with: |
85
|
|
|
* |
86
|
|
|
* ```php |
87
|
|
|
* <img src="<?= $this->publicHtml; ?>/img/luya.png" /> |
88
|
|
|
* ``` |
89
|
|
|
* |
90
|
|
|
* @return string The relative baseUrl to your public_html folder. |
91
|
|
|
*/ |
92
|
|
|
public function getPublicHtml() |
93
|
|
|
{ |
94
|
|
|
return Yii::$app->request->baseUrl; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|