Yii2: registerJs, registerCss (etc) after View::endBody()

Created on 13 Oct 2020  路  7Comments  路  Source: yiisoft/yii2

Hi!

I suggest throwing an exception or calling \Yii::warning or \Yii::error when functions yii\web\View::registerJs, yii\web\View::registerCss etc are called after function yii\web\View::endBody() was called.

Now there are any checks of Application or View states.

Some example - you can call registerJs() after Application::EVENT_AFTER_REQUEST. Any JS will be added to response.

ready for adoption enhancement

Most helpful comment

If my request contains some "hard" logic (many db queries, some http requests to another hosts etc) I use some trick - on Application::EVENT_AFTER_REQUEST I call another php process (passing args via cache or db) and run my logic there (in console application).

Sometimes I forget that on this application state I can't use registering assets and it's really hard to debug why JS files were not atached. Some warning will be useful.

All 7 comments

Sounds alright. But may I wonder how did you experience that?

If my request contains some "hard" logic (many db queries, some http requests to another hosts etc) I use some trick - on Application::EVENT_AFTER_REQUEST I call another php process (passing args via cache or db) and run my logic there (in console application).

Sometimes I forget that on this application state I can't use registering assets and it's really hard to debug why JS files were not atached. Some warning will be useful.

Formarly sounds reasonable but applies limits to those rare justified cases I've seen around.
So this would be either an error or an intention of a developer - in first case framework should not apply BC breaking limits, in second case framework should not attempt to solve developers mistake. It's edge case.

I wonder if registering anything after request could be a valid use-case...

@yiisoft/reviewers, @yiisoft/core-developers, @yiisoft/yii2 what do you think?

I agree with @lubosdz - even though it is a valid issue it's not common and should be resolved by the developer.

I think the wish of the OP is not to support this behaviour, but to throw some warning when this happens.
Just throwing a notice should not be a BC?

Yes, warning could be added, indeed.

Was this page helpful?
0 / 5 - 0 ratings