From b757d25c4b340165aacee6fc16f50bf8b3faf487 Mon Sep 17 00:00:00 2001 From: Aleksandr Bogatikov Date: Sat, 23 Oct 2021 20:08:33 +0700 Subject: [PATCH] Fix #18328: Raise warning when trying to register a file after `View::endBody()` has been called --- framework/CHANGELOG.md | 1 + framework/web/View.php | 12 ++++++++++++ 2 files changed, 13 insertions(+) diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index 1fa3646..d12bdce 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -4,6 +4,7 @@ Yii Framework 2 Change Log 2.0.44 under development ------------------------ +- Enh #18328: Raise warning when trying to register a file after `View::endBody()` has been called (perlexed) - Enh #18812: Added error messages and optimized "error" methods in `yii\helpers\BaseJson` (WinterSilence, samdark) - Chg #18823: Rollback changes #18806 in `yii\validators\ExistValidator::checkTargetRelationExistence()` (WinterSilence) - Enh #18826: Add ability to turn the sorting off for a clicked column in GridView with multisort (ditibal) diff --git a/framework/web/View.php b/framework/web/View.php index 3d2f1e6..affd8eb 100644 --- a/framework/web/View.php +++ b/framework/web/View.php @@ -133,6 +133,12 @@ class View extends \yii\base\View private $_assetManager; + /** + * Whether [[endBody()]] has been called + * @var bool + */ + private $_isBodyEnded = false; + /** * Marks the position of an HTML head section. @@ -159,6 +165,8 @@ class View extends \yii\base\View $this->trigger(self::EVENT_END_BODY); echo self::PH_BODY_END; + $this->_isBodyEnded = true; + foreach (array_keys($this->assetBundles) as $bundle) { $this->registerAssetFiles($bundle); } @@ -490,6 +498,10 @@ class View extends \yii\base\View } $appendTimestamp = ArrayHelper::remove($options, 'appendTimestamp', $assetManagerAppendTimestamp); + if ($this->_isBodyEnded) { + Yii::warning('You\'re trying to register a file after View::endBody() has been called'); + } + if (empty($depends)) { // register directly without AssetManager if ($appendTimestamp && Url::isRelative($url)) {