How to change default layout for ALL controllers in Yii2? - php

How do I change the default layout globally (= for all controllers and views) in Yii2? I want to leave the main.php layout as is in case I want to use it later.

In root of configuration you can write default layout [[\yii\base\Application::$layout]] for all views:
[
...
'layout' => 'main',
'components' => [
...
]
]

Configure layout & layoutPath in Yii2 config:
$config = [
...
'layoutPath' => '#app/views/layouts2',
'layout' => 'main2.php',
];
Above example changes default view app/views/layouts/main.php to app/views/layouts2/main2.php.
Yii2 Application Structure > Application

You can do in the following way. For example a defaultLayout.php layout can be created like this:
<?php $this->beginContent('#app/views/layouts/main.php'); ?>
<div class="container">
<div class="row">
<div class="col-lg-4">Left Side Bar</div>
<div id="content" class="col-lg-4">
<?php echo $content; ?>
</div><!-- content -->
<div class="col-lg-4">Right Side Bar</div>
</div>
</div>
<?php $this->endContent(); ?>
Inside the relative action
public function actionIndex()
{
$this->layout = 'defaultLayout';
return $this->render('index', [
'model' =>$model,
]);
}
In configuration(config/main.php) you can overwrite the default layout for all your views
[
// ...
'components' => [
'view' => [
'layout' => 'main.php'
],
// ...
],
]

In your config, you can edit the layoutPath .
Example:
$config = [
...
'layoutPath' => '#app/views/layouts-2'
];

Change in config/web.php file
and add this line before components array
your new style name which is created in view/layout/main_style.php
'layout' => 'main_style'
Using this whole project layout changed

Related

Customizing the Templates FormHelper for just plugins CakePHP 3?

I would like to customize a form helper template for just a plugin, not across the App.
Example: 'inputContainer' => '<div class="form-control">{{content}}</div>',
How do I go about doing this in CakePHP 3?
As per the docs - to change theme inline at runtime use setTemplate:
$myTemplates = [
'inputContainer' => '<div class="form-control">{{content}}</div>',
];
?>
<?= $this->Form->create('Users') ?>
<? $this->Form->setTemplates($myTemplates); ?>
<?=
$this->Form->input('email', [
'class' => 'form-control',
'templates' => [
'formGroup' => '{{input}}{{label}}'
]])
?>
You can't use the templates option on the Form for a dynamic template, it will only look for a config file listing template items in /config.

ZF2 - Dynamic ViewHelper

I want to create a view helper which should return some HTML like that:
<div class="panel panel-default">
<div class="panel-body">
:: Here is my content ::
</div>
</div>
The plan is to call it like the following, where the parameter should be a key in my config, which defines a view helper class to generate the content of my panel:
<?php
echo $this->panel('test');
$config = [
'view_helpers' => [
'factories' => [
'Panel' => 'Application\View\Helper\Factory\PanelFactory',
'Test' => 'Application\View\Helper\Factory\TestFactory'
]
],
'panels' => [
'test' => 'Test'
]
];
?>
So I could load every content of a custom view helper into a panel, but I would have to inject the view helper manager in my panel view helper to load the wanted view helper from my config. Would that be correct or is there a better solution?
Maybe you should have a look at ZF2 partials. If I am not mistaken the partial view helper seems to be doing what you want.
You define a template (panel.phtml):
<div class="panel panel-default">
<div class="panel-body">
<?php echo $this->content ?>
</div>
</div>
In the view where you want to output your panel partial:
$this->partial('panel.phtml', array(
'content' => 'Whatever content you prefer (can also be another partial)'
);
You can register your partials like you would register any other view inside your template map in the view helper config:
'view_manager' => array(
'template_map' => array(
'layout/layout' => __DIR__ . '/../view/layout/layout.phtml',
'header' => __DIR__ . '/../view/layout/header.phtml',
'footer' => __DIR__ . '/../view/layout/footer.phtml',
'error/404' => __DIR__ . '/../view/error/404.phtml',
'error/index' => __DIR__ . '/../view/error/index.phtml',
'panel' => __DIR__ . '/../view/partial/panel.phtml',
'test' => __DIR__ . '/../view/partial/test.phtml',
)
)
Seems an easy solution to what you want to achieve.
Or do you have other demands?

Override Yii2 assetManager config in controller

I use yii-jui to add some UI elements in the views such as datePicker. In the frontend\config\main-local.php I set the following to change the theme used by the JqueryUI:
$config = [
'components' => [
'request' => [
// !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
'cookieValidationKey' => 'gjhgjhghjg87hjh8878878',
],
'assetManager' => [
'bundles' => [
'yii\jui\JuiAsset' => [
'css' =>
['themes/flick/jquery-ui.css'],
],
],
],
],
];
I tried the following to override this configuration item in the controller actions method:
public function actions() {
Yii::$app->components['assetManager'] = [
'bundles' => [
'yii\jui\JuiAsset' => [
'css' =>
['themes/dot-luv/jquery-ui.css'],
],
],
];
return parent::actions();
}
Also I tried to set the value of Yii::$app->components['assetManager'] shown above to the view itself (it is partial view of form _form.php) and to the action that calls this view (updateAction). However, all this trying doesn't be succeeded to change the theme. Is there in Yii2 a method like that found in CakePHP such as Configure::write($key, $value);?
You should modify Yii::$app->assetManager->bundles (Yii::$app->assetManager is an object, not an array), e.g.
Yii::$app->assetManager->bundles = [
'yii\jui\JuiAsset' => [
'css' => ['themes/dot-luv/jquery-ui.css'],
],
];
Or if you want to keep other bundles config :
Yii::$app->assetManager->bundles['yii\jui\JuiAsset'] = [
'css' => ['themes/dot-luv/jquery-ui.css'],
];
You are going about this all wrong, you want to change the JUI theme for 1 controller alone because of a few controls. You are applying 2 css files to different parts of the website that have the potential to change styles in the layouts too. The solution you found works but it is incredibly bad practice.
If you want to change just some controls do it the proper way by using JUI scopes.
Here are some links that will help you:
http://www.filamentgroup.com/lab/using-multiple-jquery-ui-themes-on-a-single-page.html
http://jqueryui.com/download/
In this way you are making the website easier to maintain and you do not create a bigger problem for the future than you what solve.

Yii2: Failed creating own template with GII module (advance template)

I have some problem here
I have created an advance template with yii2, and i've followed "The Gii code generation tool" from http://www.yiiframework.com/doc-2.0/guide-gii.html
copy folder default template
cp [YII_ROOT]\vendor\yiisoft\yii2-gii\generators\crud\default
[YII_ROOT]\backend\generator\crud
edit [YII_ROOT]\backend\generator\crud\default\views\_form.php
<?= "<?php " ?>$form = ActiveForm::begin(); ?>
<?= "<?=" ?> $form->errorSummary($model) ?> <!-- ADDED HERE -->
<?php foreach ($generator->getColumnNames() as $attribute) {
if (in_array($attribute, $safeAttributes)) {
echo " <?= " . $generator->generateActiveField($attribute) . " ?>\n\n";
}
} ?>
edit [YII_ROOT]\backend\main.php
return [
'bootstrap' => ['gii'],
'modules' => [ 'gii' => [
'class' => 'yii\gii\Module',
'generators' => [ //here
'crud' => [ //name generator
'class' => 'yii\gii\generators\crud\Generator', //class generator
'templates' => [ //setting for out templates
'myTemplate' => '#app\generator\crud\default', //name template => path to template
]
]
],
],
], ];
generate with gii (in this case generate CRUD)
The problem is, i still can't find the difference of _form.php template. what should i do?

Remove 'home' link from breadcrumb

I have breadcrumbs such as Home > Instance > Action in all my view pages. How can I remove the 'Home' link from all the breadcrumbs?
$this->breadcrumbs=array(
'Keypairs'=>array('admin'),
'Manage',
);
This can be done by setting the homeLink property to false, in your CBreadcrumbs widget initialization. This is usually done in a layout file.
In the default Yii app, in protected/views/layouts/main.php:
<?php if(isset($this->breadcrumbs)):?>
<?php $this->widget('zii.widgets.CBreadcrumbs', array(
'links'=>$this->breadcrumbs,
'homeLink'=>false // add this line
)); ?><!-- breadcrumbs -->
<?php endif?>
in layouts/main.php add 'homeLink' => false
<?= Breadcrumbs::widget([
'links' => isset($this->params['breadcrumbs'])? $this->params['breadcrumbs'] : [],
'homeLink' => false
]) ?>

Categories