How to use faker in yii2 in advance template - yii2

I am generating random data for project and for unit testing. So i want to use faker. But unable to how to use faker in yii2?

set this in your main config:
return [
'controllerMap' => [
'fixture' => [
'class' => 'yii\faker\FixtureController',
'templatePath' => '#common/tests/templates/fixtures',
'fixtureDataPath' => '#common/tests/fixtures/data',
],
// ...
],
// ...
];
use it like this:
$faker = Faker\Factory::create();
// generate data by accessing properties
echo $faker->name;
// 'Lucy Cechtelar';
echo $faker->address;
// "426 Jordy Lodge
// Cartwrightshire, SC 88120-6700"
echo $faker->text;
for more info check faker basic usage
and yii2 guide

Related

Multiple Paths for img and css in CakePHP 3.4

I am running multiple cake apps which share one core/baseapp.
The basic structure is:
www/myapp1
www/myapp2
www/myapp3
www/baseapp
In each 'myapp' inside app.php I point the path for templates and plugins to the myapp and baseapp so that if no file is found in myapp the one from baseapp is used:
'App' => [
'imageBaseUrl' => 'img/',
'cssBaseUrl' => 'css/',
'jsBaseUrl' => 'js/',
'paths' => [
'plugins' => [ROOT.DS .'modules'.DS, BASEROOT . DS . 'modules' . DS],
'templates' => [APP . 'Template' . DS, BASEROOT.'/src/Template'.DS ],
],
],
(i include classes from baseapp inside composer.json which is probably not relevant for this question)
That works pretty well.
Question:
How can I make it include img, css and js in the same manner?
I want to have to folders where cake should search for the files for every app.
'App' => [ 'imageBaseUrl' => ['img/', BASEROOT. DS . 'webroot/img/']
does not work.
My Solution: In the folder webroot I made a link to BASEROOT (with the name 'baseroot') and then I made the File src/View/UrlHelper
class UrlHelper extends CakeUrlHelper{
public function webroot($file){
//code
if (file_exists(\Cake\Core\Configure::read('App.wwwRoot') . $file)) {
$webPath = $this->request->getAttribute('webroot') . $asset[0];
}else{
$webPath = $this->request->getAttribute('webroot').'baseroot'.DS.$asset[0];
}
//more code
}
}

Class Swift_Transport_Esmtp_Auth_CramMd5Authenticator does not exist

I can't seem to send an email with laravel 5.4 on after setting the right configurations in config/mail.php. I'm using smtp driver here is summary of my files.
Here is the config/mail.php
<?php
return [
'driver'=>env('MAIL_DRIVER', 'smtp'),
'host'=>env('MAIL_HOST', 'smtp.gmail.com'),
'port' => env('MAIL_PORT', 587),
'from' => [
'address' => env('MAIL_FROM_ADDRESS', 'info#afecamworld.org'),
'name' => env('MAIL_FROM_NAME', 'National President'),
],
'encryption' => env('MAIL_ENCRYPTION', 'ssl'),
'username' => env('MAIL_USERNAME', 'my-gmail-email'),
'password' => env('MAIL_PASSWORD', 'my-gmail-password'),
'sendmail' => '/usr/sbin/sendmail -bs',
'markdown' => [
'theme' => 'default',
'paths' => [
resource_path('views/vendor/mail'),
],
],
This is the SendActivationEmail class
<?php
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
class SendActivationEmail extends Notification {
use Queueable;
protected $token;
public function __construct($token) {
$this->token = $token;
}
public function via($notifiable){
return ['mail', 'database'];
}
public function toMail($notifiable) {
return (new MailMessage)->subject('Activation Your Account')->greeting('Hello!')->line('You need to activate your email before you can login.')->action('Activate Email', route('activate_account', ['token' => $this->token]))->line('Thank you for joining our Online Community!');
}
}
?>
This is the top part of the error
(1/1) ReflectionException
Class Swift_Transport_Esmtp_Auth_CramMd5Authenticator does not exist
in DependencyContainer.php (line 309)
I will be grateful for any help to resolve this error on dreamhost, thanks.
The best way to resolve this problem is to delete the files in the vendor folder and redo composer install and composer update, that's how I fixed my problem

change url for modules in Yii2

Is there an easy way to change the default url for a module? I have created an Admin module but I don't want it to reside at /admin as that is easy to guess and I'd like to host it at a more obscure path like /c3ntgu33sthis but don't want to create a module called c3ntgu33sthis, plus I'd like to change the url once in a while.
When configuring your module you can set any name for it you like and change it later on.
I assume you have got something like:
'modules' => [
// ...
'admin' => [
'class' => 'module\namespace\for\Admin'
],
],
Change it to:
'modules' => [
// ...
'c3ntgu33sthis' => [
'class' => 'module\namespace\for\Admin'
],
],

How to set logFile path dynamically for different modules in yii

I want to set different logpath for each module so that all errors, warnings, should be categorize with module name as parent folder.
i.e
runtime/logs/module_name/errors.log
runtime/logs/module_name/warnings.log
runtime/logs/module_name/info.log.
currently log component is configured in config/main.php
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'flushInterval' => 1,
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['info'],
'categories' => ['common\modules\*'],
'logFile' => '#runtime/logs/common.modules.info.log',
],
],
]
I want to dynamically assign logFile path value in above application configuration file, so that if any error, warning, info is called in any module it will automatically pick that module name and logs the information.
You can add log target dynamically from controller or module init.
$target = new FileTarget();
$target->logFile = \Yii::getAlias('#runtime') . '/custom.log'
$target->categories = ['log-category']
\Yii::$app->getLog()->targets = [$target];
But I think will be better create new FileTarget class and moved static settings like categories into it.

Yii2 kartik-datecontrol extension

I installed the above extension via composer and follow the documentation for every step; in my :
view : use kartik\datecontrol\DateControl;// <?=$form->field($model, 'dated')->widget(DateControl::classname(), [
'type'=>DateControl::FORMAT_DATE,
'ajaxConversion'=>false,
'options' => ['pluginOptions' => ['autoclose' => true ],'class'=>'col-xs-12 form-control input-sm']])?>
Web.php - Module configuration :
use \kartik\datecontrol\Module;
'datecontrol' => [
'class' => 'kartik\datecontrol\Module',
// format settings for displaying each date attribute (ICU format example)
'displaySettings' => [
Module::FORMAT_DATE => 'php:dd-m-Y',
Module::FORMAT_TIME => 'php:H:i:s',
Module::FORMAT_DATETIME => 'php:Y-m-d H:i:s',
],
// format settings for saving each date attribute (PHP format example)
'saveSettings' => [
Module::FORMAT_DATE => 'php:Y-m-d',
Module::FORMAT_TIME => 'php:H:i:s',
Module::FORMAT_DATETIME => 'php:Y-m-d H:i:s',
],
// set your display timezone
// 'displayTimezone' => 'Asia/Kolkata',
// set your timezone for date saved to db
// 'saveTimezone' => 'UTC',
// automatically use kartik\widgets for each of the above formats
'autoWidget' => true,
// default settings for each widget from kartik\widgets used when autoWidget is true
'autoWidgetSettings' => [
Module::FORMAT_DATE => ['type'=>2, 'pluginOptions'=>['autoclose'=>true]], // example
Module::FORMAT_DATETIME => [], // setup if needed
Module::FORMAT_TIME => [], // setup if needed
],
// custom widget settings that will be used to render the date input instead of kartik\widgets,
// this will be used when autoWidget is set to false at module or widget level.
'widgetSettings' => [
Module::FORMAT_DATE => [
'class' => 'yii\jui\DatePicker', // example
'options' => [
'dateFormat' => 'php:d-M-Y',
'options' => ['class'=>'form-control'],
]
]
]
// other settings
]
When I run the view I got the following error message :
{"name":"Invalid Configuration","message":"The class
'\kartik\date\DatePicker' was not found and is required for
DateControl 'date' format.\n\nPlease ensure you have installed one of
'yii2-widgets' OR 'yii2-widget-datepicker' extensions. To install, you
can run this console command from your application root:\n\nphp
composer.phar require kartik-v/yii2-widgets: \"#dev\"\n\n--- OR
---\n\nphp composer.phar require kartik-v/yii2-widget-datepicker: \"#dev\"","code":0,"type":"yii\base\InvalidConfigException","file":"C:\wamp\www\pub\vendor\kartik-v\yii2-krajee-base\Config.php","line":118,"stack-trace":["#0
C:\wamp\www\pub\vendor\kartik-v\yii2-krajee-base\Config.php(195):
kartik\base\Config::checkDependency('\\kartik\\date\\Da...',
Array, 'for DateControl...')","#1
C:\wamp\www\pub\vendor\kartik-v\yii2-datecontrol\DateControl.php(215):
kartik\base\Config::validateInputWidget('\\kartik\\date\\Da...',
'for DateControl...')","#2
C:\wamp\www\pub\vendor\kartik-v\yii2-datecontrol\DateControl.php(154):
kartik\datecontrol\DateControl->initConfig()","#3
C:\wamp\www\pub\vendor\yiisoft\yii2\base\Object.php(107):
kartik\datecontrol\DateControl->init()","#4 [internal function]:
yii\base\Object->__construct(Array)","#5
C:\wamp\www\pub\vendor\yiisoft\yii2\di\Container.php(372):
ReflectionClass->newInstanceArgs(Array)","#6
C:\wamp\www\pub\vendor\yiisoft\yii2\di\Container.php(151):
yii\di\Container->build('kartik\\datecont...', Array, Array)","#7
C:\wamp\www\pub\vendor\yiisoft\yii2\BaseYii.php(344):
yii\di\Container->get('kartik\\datecont...', Array, Array)","#8
C:\wamp\www\pub\vendor\yiisoft\yii2\base\Widget.php(97):
yii\BaseYii::createObject(Array)","#9
C:\wamp\www\pub\vendor\yiisoft\yii2\widgets\ActiveField.php(665):
yii\base\Widget::widget(Array)","#10
C:\wamp\www\pub\views\activite\schedules.php(49):
yii\widgets\ActiveField->widget('kartik\\datecont...',
Array)","#11
C:\wamp\www\pub\vendor\yiisoft\yii2\base\View.php(325):
require('C:\\wamp\\www\\pub...')","#12
C:\wamp\www\pub\vendor\yiisoft\yii2\base\View.php(247):
yii\base\View->renderPhpFile('C:\\wamp\\www\\pub...',
Array)","#13
C:\wamp\www\pub\vendor\yiisoft\yii2\base\View.php(149):
yii\base\View->renderFile('C:\\wamp\\www\\pub...', Array,
Object(app\controllers\ActiviteController))","#14
C:\wamp\www\pub\vendor\yiisoft\yii2\base\Controller.php(371):
yii\base\View->render('schedules', Array,
Object(app\controllers\ActiviteController))","#15
C:\wamp\www\pub\controllers\ActiviteController.php(407):
yii\base\Controller->render('schedules', Array)","#16 [internal
function]:
app\controllers\ActiviteController->actionAddsch('4')","#17
C:\wamp\www\pub\vendor\yiisoft\yii2\base\InlineAction.php(55):
call_user_func_array(Array, Array)","#18
C:\wamp\www\pub\vendor\yiisoft\yii2\base\Controller.php(151):
yii\base\InlineAction->runWithParams(Array)","#19
C:\wamp\www\pub\vendor\yiisoft\yii2\base\Module.php(455):
yii\base\Controller->runAction('addsch', Array)","#20
C:\wamp\www\pub\vendor\yiisoft\yii2\web\Application.php(84):
yii\base\Module->runAction('activite/addsch', Array)","#21
C:\wamp\www\pub\vendor\yiisoft\yii2\base\Application.php(375):
yii\web\Application->handleRequest(Object(yii\web\Request))","#22
C:\wamp\www\pub\web\index.php(12):
yii\base\Application->run()","#23 {main}"]}
Try installing the following widgets:
yii2-widget-datepicker
yii2-widget-datetimepicker
or better, install the kartik widgets using composer:
php composer.phar require kartik-v/yii2-widgets "*"
that will install the following widgets:
yii2-krajee-base
yii2-widget-activeform
yii2-widget-affix
yii2-widget-alert
yii2-widget-colorinput
yii2-widget-datepicker
yii2-widget-datetimepicker
yii2-widget-depdrop
yii2-widget-fileinput
yii2-widget-growl
yii2-widget-rangeinput
yii2-widget-rating
yii2-widget-select2
yii2-widget-sidenav
yii2-widget-spinner
yii2-widget-switchinput
yii2-widget-timepicker
yii2-widget-touchspin
yii2-widget-typeahead