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

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?

Related

How to pass php variable to PluginEvent config Slider Kartik extension for Yii2

I use Slider Kartik extension for Yii2. There is PluginEvent options, that i want to config to my needs. So here is my code:
<?php echo Slider::widget([
'name'=>'rating_1',
'value'=>7,
'sliderColor'=>Slider::TYPE_GREY,
'pluginEvents' => [
'slide' => "function(slideEvt) {
$('#testVal. ').text(slideEvt.value);
}",
],
]);
?>
<span>Current Slider Value: <span id="testVal">3</span></span>
But in my HTML layout i used models attribute like: $value['strategy_title']
Is there is the way to put my $value['strategy_title'] instead of #testVal?
You need to concatenate php variable,
<?= Slider::widget([
'name' => 'rating_1',
'value' => 7,
'sliderColor' => Slider::TYPE_GREY,
'pluginEvents' => [
'slide' => "function(slideEvt) {
$('#".$value['strategy_title']."').text(slideEvt.value);
}",
],
]) ?>

How to disable or remove Yii2 redactor Image and file button?

I am using Yii2 Redactor from Here. I want to remove Image and File Upload.
view Code :
<?= $form->field($model, 'reason')->widget(
\yii\redactor\widgets\Redactor::className(), [])
?>
Screenshot
If you want to hide the buttons for all instances of Redactor you can add this into the module config
'modules' => [
'redactor' => [
'class' => 'yii\redactor\RedactorModule',
'widgetClientOptions' => [
'buttonsHide' => ['image','file'],
]
],
],
Otherwise you can add this to the individual call
<?= $form->field($model, 'reason')->widget(\yii\redactor\widgets\Redactor::className(), [
'clientOptions' => [
'buttonsHide' => ['image','file'],
]
])?>
In this library, you just have to update file i.e. Redactor.php at path
yii2-redactor/widgets/Redactor.php
Now update method defaultOptions.
Comment or Remove line from 92 to 103
Here is code of these lines:
$this->setOptionsKey('imageUpload', $this->module->imageUploadRoute);
$this->setOptionsKey('fileUpload', $this->module->fileUploadRoute);
$this->clientOptions['imageUploadErrorCallback'] = ArrayHelper::getValue($this->clientOptions, 'imageUploadErrorCallback', new JsExpression("function(json){alert(json.error);}"));
$this->clientOptions['fileUploadErrorCallback'] = ArrayHelper::getValue($this->clientOptions, 'fileUploadErrorCallback', new JsExpression("function(json){alert(json.error);}"));
if (isset($this->clientOptions['plugins']) && array_search('imagemanager', $this->clientOptions['plugins']) !== false) {
$this->setOptionsKey('imageManagerJson', $this->module->imageManagerJsonRoute);
}
if (isset($this->clientOptions['plugins']) && array_search('filemanager', $this->clientOptions['plugins']) !== false) {
$this->setOptionsKey('fileManagerJson', $this->module->fileManagerJsonRoute);
}

Images are not saved into the photo_dir folder using cakephp-upload plugin on CakePHP 3.2

I am using the cakephp-upload plugin and I managed to upload images to my server:
WorkersTable:
public function initialize(array $config)
{
parent::initialize($config);
$this->table('workers');
$this->displayField('id');
$this->primaryKey('id');
$this->addBehavior('Josegonzalez/Upload.Upload', [
'avatar' => [
'fields' => [
'dir' => 'photo_dir'
]
]
]);
}
view.ctp:
echo $this->Form->create($newWorker, ['type' => 'file']);
echo $this->Form->input('avatar', ['type' => 'file']);
echo $this->Form->input('photo_dir', ['type' => 'hidden']);
Now the avatar images are uploaded, but they are not put into the photo_dir subdirectory.
What am I missing? It works without any problems in my CakePHP 2.8.x application.
Author of the plugin here.
The fields.dir attribute does not state what the subdirectory should be. It is a reference to the column in your database table where we should save the director where we saved the file.
If you want to change the place where you save files on disk, you should instead use the path option. Here is an example where i use the photo_dir subdirectory:
$this->addBehavior('Josegonzalez/Upload.Upload', [
'avatar' => [
'path' => 'webroot{DS}files{DS}{model}{DS}{field}{DS}photo_dir{DS}'
]
]);
The default value for the path option is webroot{DS}files{DS}{model}{DS}{field}{DS}.
Shouldn't it be:
$this->addBehavior('Josegonzalez/Upload.Upload', [
'avatar' => [
'fields' => [
'dir' => 'avatar_dir'
]
]
]);
echo $this->Form->input('avatar_dir', ['type' => 'hidden']);
If you want to use better option than you can use below plugin which has better option for upload the file rather than Josegonzalez/Upload.Upload plugin.
I have used below one in my project.
Utils Plugin for Cake 3.x
Link for this plugin : https://github.com/cakemanager/cakephp-utils
Documentation : http://cakemanager.org/docs/utils/1.0/behaviors/uploadable/
And this is the configuration :
$this->addBehavior('Utils.Uploadable', [
'image' => [
'path' => '{ROOT}{DS}{WEBROOT}{DS}uploads{DS}{field}{DS}',
'fileName' => md5(rand(1000, 5000000)) . '.{extension}',
'removeFileOnDelete' => true,
'removeFileOnUpdate' => FALSE
],
]);
Here you can customise it. Let me know if you have any question regarding this.

How to change default layout for ALL controllers in Yii2?

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

How to get yii2 registered assets list for creating SPF.js response array?

I need getting list of yii2 assets registered in the page for creating an array for response SPF.js requests
I use this code in a new layout named ajax.php
<?php
use yii\helpers\Html;
use app\assets\AppAsset;
use \yii\helpers\Json;
AppAsset::register($this);
$spf_data = [
'title' => Html::encode($this->title),
'head' => $this->beginPage() . Html::csrfMetaTags() . $this->head(),
'body' => ['content' => $this->beginBody() . $content],
"attr" => [
'content' => [
"class" => "container"
]
],
'foot' => $this->endBody() . $this->endPage(),
];
echo Json::htmlEncode($spf_data);
my problem is registered assets to page and i can't get them in my array
i need to get list of meta tags,link tags and script in head index of array
how can I do?
You can get scripts and styles in your controller action with this code
ob_start();
ob_implicit_flush(false);
$this->view->beginPage();
$this->view->head();
$this->view->beginBody();
$this->renderPartial($view, $params);
$this->view->endBody();
$this->view->endPage(true);
$style_script= ob_get_clean();
and use $style_script in $spf_data
$spf_data = [
'title' => Html::encode($this->title),
'head' => $style_script,
'body' => ['content' => $this->renderPartial('viewName')],
"attr" => [
'content' => [
"class" => "container"
]
],
];
this code work for me

Categories