Sometimes when we use ErrorAction, we need use different layout, but now it's little bit difficult to do it easily, so this small new feature help to solve problem
public function actions()
{
return [
'error' => [
'class' => 'yii\web\ErrorAction',
'layout' => 'error'
],
];
}
| Q | A
| ---------------- | ---
| Yii version | 2.0.12
| PHP version | 7.1
| Operating system | Winux
Makes absolutely no sense. Changing layout inside the view file is easy:
<?php
// File @app/views/site/error.php
$this->context->layout = 'error-layout';
漏 The removal of the tonsils through the anus - best
@klimov-paul It can be configured in application config, leaving the original view and only changing the layout.
It can be configured in application config, leaving the original view and only changing the layout.
What the sense of it?
How many other paramters we should add to error config to be enough?
May be we should add 'page title' for example, because setting lines line:
$this->title = 'Error page';
is too complicated for some individuals, or because it will allow to set page title 'leaving the original view and only changing the title'?
View file is able to handle all necessary conficugration relate to its rendering already, that is why there are no more options regardless to them.
why you use $this->title = 'Error page';
as exp. it's View property, layout - Controller property
When you need to turn off Controller::$enableCsrfValidation
for some action you also use $this->context->enableCsrfValidation = false;
?
Your solution is very difficult for intuitive understanding. When somebody start to read the code, first attention there set the layout is in the controller NOT in the view
Your solution is very difficult for intuitive understanding. When somebody start to read the code, first attention there set the layout is in the controller NOT in where view
View is rendered in the context of the controller. This is explainded in the guide:
https://github.com/yiisoft/yii2/blob/master/docs/guide/structure-views.md#named-views-
https://github.com/yiisoft/yii2/blob/master/docs/guide/structure-views.md#accessing-data-in-views-
It is normal to access or adjust context from the view scope - that is why context
is exposed to it.
why you use $this->title = 'Error page'; as exp. it's View property, layout - Controller property
When you need to turn off Controller::$enableCsrfValidation for some action you also use $this->context->enableCsrfValidation = false;?
Layout is matter of representation - it does not affect user-interaction.
It is absolutely fine for the view to determine its layout.
This property is exposed to a controller, because of the 'context' necessity: someone should define where the view files are located. View and its context should interact and thus may affect earch other.
Your fix #14644 is also inappropriate in the scope of your statement, as you set $layout
property to the Action
instance, why normally it has no such property at all, so it will postpone it to its controller owner overridding any existing present of the Controller::$layout
. So rephrasing your question: should I use Action
instance in case I wish to change Controller::$enableCsrfValidation
property?
It is unlikely someone to set Controller::$enableCsrfValidation
inside the view file as it make a little sense - it will not affect request processing, which is complete at this stage.
Adjustments of the layout as well as page title (which appears only in the layout file by the way) still will have effect on the page rendering - it is the matter of representation layer, thus it is fine to be kept inside the representation file.
Your ignorance is not an excuse for creating redundant shortcut code.
View's title property is absolutely about representation layer and must be inside view file.
Default layout is good for controller-wide configuration, any specific layout I prefer to write in action, but, layout is definitely about representation layer too.
So, redefine layout in view, is a good way.
Add property to Error Action without having it in Action is very bad idea.
This makes code real more complex and hard to understand, when someone, using layout property in ErrorAction will not find it in Action class.
Also looking for same kind of solution: my case is that I have to use 2 layouts in 1 controller.
I just use $this->layout = 'custom_layout';
in a few actions. For me it's nesessary, that even errors for those actions to be displayed in custom layout. But some type of errors happens before action starts (in behavior in my case), and the error is beeing displayed in default layout, not in custom.
+1 for authors request for this issue
Write below function in your default controller -
public function beforeAction($action) {
if (parent::beforeAction($action)) {
// change layout for error action
if ($action->id == 'error')
$this->layout = 'custom_error_layout';
return true;
} else {
return false;
}
}
Most helpful comment
@klimov-paul It can be configured in application config, leaving the original view and only changing the layout.