Yii2 module or controller routing - yii2

I try use routing for module and controller
'<controller>'=>'<controller>/index',
'<controller>/<id:\d+>'=>'<controller>/view',
'<controller>/<action>'=>'<controller>/<action>',
'<module>' => '<module>/default/index',
'<module>/<id:\d+>' => '<module>/default/view',
'<module>/edit/<id:\d+>' => '<module>/default/edit',
'<module>/delete/<id:\d+>' => '<module>/default/delete',
'<module>/<action>' => '<module>/default/<action>',
'<module>/<controller>' => '<module>/<controller>/index',
'<module>/<controller>/edit/<id:\d+>' => '<module>/<controller>/edit',
'<module>/<controller>/delete/<id:\d+>' => '<module>/<controller>/delete',
'<module>/<controller>/<id:\d+>' => '<module>/<controller>/view',
'<module>/<controller>/<action>' => '<module>/<controller>/<action>',
but for some reason, if the first matching rule did not work, then the rest are not checked, you get a 404 error.
For example, if the controller is not found, then 404 is obtained, and the check does not reach the modules.
url manager config
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'store/upload'=>'store/upload',
'stores'=>'store/index',
// 'post/<id:\d+>'=>'post/view',
'comment/send'=>'comment/send',
['class' => 'yii\rest\UrlRule', 'controller' => 'Api', 'prefix' => 'api'],
'api/<id:\d+>'=>'api/view',
[
'pattern' => 'users',
'route' => 'users/index',
'suffix' => '/',
'normalizer' => false, // отключаем нормализатор для этого правила
],
// '<controller>'=>'<controller>/index',
// '<controller>/<id:\d+>'=>'<controller>/view',
// '<controller>/<action>'=>'<controller>/<action>',
'<module>' => '<module>/default/index',
'<module>/<id:\d+>' => '<module>/default/view',
'<module>/edit/<id:\d+>' => '<module>/default/edit',
'<module>/delete/<id:\d+>' => '<module>/default/delete',
'<module>/<action>' => '<module>/default/<action>',
'<module>/<controller>' => '<module>/<controller>/index',
'<module>/<controller>/edit/<id:\d+>' => '<module>/<controller>/edit',
'<module>/<controller>/delete/<id:\d+>' => '<module>/<controller>/delete',
'<module>/<controller>/<id:\d+>' => '<module>/<controller>/view',
'<module>/<controller>/<action>' => '<module>/<controller>/<action>',
],
],
Request Url http://yii2/post
To PostController/actionIndex()

I fix routing, It work.The controllers are checked first, then the modules.
'<controller:\w+>'=>'<controller>/index',
'<controller:\w+>/<id:\d+>'=>'<controller>/view',
'<controller:\w+>/<action>'=>'<controller>/<action>',
'<module:\w+>' => '<module>/default/index',
'<module:\w+>/<id:\d+>' => '<module>/default/view',
'<module:\w+>/edit/<id:\d+>' => '<module>/default/edit',
'<module:\w+>/delete/<id:\d+>' => '<module>/default/delete',
'<module:\w+>/<action:\w+>' => '<module>/default/<action>',
'<module:\w+>/<controller:\w+>' => '<module>/<controller>/index',
'<module:\w+>/<controller:\w+>/edit/<id:\d+>' => '<module>/<controller>/edit',
'<module:\w+>/<controller:\w+>/delete/<id:\d+>' => '<module>/<controller>/delete',
'<module:\w+>/<controller:\w+>/<id:\d+>' => '<module>/<controller>/view',
'<module:\w+>/<controller:\w+>/<action>' => '<module>/<controller>/<action>',

Related

Yii2 captcha always incorrect

Whan I add 'on' => 'create' in my rule my captcha always incorrect
Off that work good why?
I try to let it to other controller support/captcha but still incorrect
MyView
<?= Captcha::widget([
'id' => 'captcha',
'model' => $model,
'attribute' => 'verifyCode',
'options' => ['class' => 'form-control',
'data-v-rule' => '',
'captchaAction' => 'support/captcha',
'data-v-msg' => $i18n->t('please.input.verify.code'),
'placeholder' => $i18n->t('please.input.verify.code', '', false)]
]); ?>
Myrule
...
[['verifyCode'], 'captcha', 'message' => $i18n->t('error.verifyCode'),'captchaAction' => 'support/captcha' , 'on' => 'create'],
...
I get seesion
$_SESSION = [
'__flash' => [],
'__captcha/site/captcha' => 'nnbioo',
'__captcha/site/captchacount' => 1,
'__captcha/support/captcha' => 'cacijq',
'__captcha/support/captchacount' => 1,
];
Mycontroller
public function actions()
{
return [
'error' => [
'class' => 'yii\web\ErrorAction',
],
'captcha' => [
'class' => CaptchaAction::className(),
],
];
}
$customerServiceModel->load($post) is not working, because attribute verifyCode is not safe in this case. Your rule is specified with 'on' => 'create' - so it means, it's save on scenario create. You didn't assigned this scenario to the model so there's 2 solutions:
Remove 'on' => 'create' from rule
Assign create scenario to model by $customerServiceModel->scenario = 'create'; before using load().

How to debug flysystem

I install Yii 2 File kit extendsion, and set local file storage success.
Then i config SFTP then run command
Yii::$app->fileStorage1->getFilesystem()->createDir('demo')
So it not work. and only show result false.
So i don't know what the error.
I try conffig:
'fileStorage1' => [
'class' => '\trntv\filekit\Storage',
//'baseUrl' => '#storageUrl1',
//'filesystemComponent' => 'fs'
'filesystemComponent' => 'sftpFs'
],
'sftpFs' => [
'class' => 'creocoder\flysystem\SftpFilesystem',
'host' => 'Myserver',
'port' => 22,
'username' => 'u',
'password' => 'p',
//'privateKey' => '/path/to/or/contents/of/privatekey',
// 'timeout' => 60,
'root' => 'public_html',
// 'permPrivate' => 0700,
// 'permPublic' => 0744,
],

Yii2 sort object defaultOrder

I have a table of policies that belong to chapters. When I view the policies in my grid view I want the default order be policy title within chapter title. I see how to set up sort attributes to enable this, but I can't figure out how to set the defaultOrder to be based on chapter title and then policy title. When ever I try to set policy.title as an attribute in the defaultOrder setting I get an error.
If Policy is model class of policy table that has a relation with chapter table named 'chapter' and linked by chapter_id field, such as:
public function getChapter()
{
return $this->hasOne(Chapter::className(), ['chapter_id' => 'chapter_id']);
}
Now you build query object with policy joined with chapter:
$query = Policy::find()
->joinWith(['chapter']);
$dataProvider = new ActiveDataProvider([
'query' => $query,
'sort'=> ['defaultOrder' => ['chapter.title'=>SORT_ASC, 'policy.title' => SORT_ASC]]
]);
I could not get Fabrizio's answer to work, because my chapter table is policy_chapter, not just chapter and so is not the same as the relation name. When I tried to use the relation name, I got errors. I finally figured out in the sort, you have to use the names of the related tables, not the relations, and add them as attributes. e.g.:
$dataProvider = new ActiveDataProvider([
'query' => $query,
'pagination' => ['pageSize' => 50],
'sort' => [
'defaultOrder' => ['policy_chapter.sort' => SORT_ASC,'policy_chapter.title' => SORT_ASC],
'enableMultiSort' => true,
'attributes' => [
'id' => [
'asc' => ['policy.id' => SORT_ASC],
'desc' => ['policy.id' => SORT_DESC],
'default' => SORT_ASC
],
'chapter_id' => [
'asc' => ['policy_chapter.sort' => SORT_ASC,'policy_chapter.title' => SORT_ASC],
'desc' => ['policy_chapter.sort' => SORT_DESC,'policy_chapter.title' => SORT_DESC],
'default' => SORT_ASC,
],
'reference' => [
'asc' => ['reference' => SORT_ASC],
'desc' => ['reference' => SORT_DESC],
'default' => SORT_ASC
],
'title' => [
'asc' => ['policy.title' => SORT_ASC],
'desc' => ['policy.title' => SORT_DESC],
'default' => SORT_ASC
],
'policy_chapter.sort',
'policy_chapter.title',
],
],
]);
Use the relation name or leave out the attributes and you get errors. Before I was writing
'defaultOrder' => ['policy_chapter.sort' => SORT_ASC,'policy_chapter.title' => SORT_ASC],
and Yii was not happy

Yii2 Mailer isn't actually sending emails

I followed a Yii2 tutorial for sending e-mails with attachment,
(See the link below if you need)
https://www.youtube.com/watch?v=c5pebmTUQjs&index=21&list=PLRd0zhQj3CBmusDbBzFgg3H20VxLx2mkF
And it worked on a level that the system reads the email info and the attachment that I want to send, and saves the info and attachment link in the db.
But when I tried to send emails to a real recipient and changed the mailer config, then tried to create a new email
there was a problem:
Invalid Configuration – yii\base\InvalidConfigException
Setting unknown property: Swift_MailTransport::host
Here is the config for mailer:
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'viewPath' => '#app/mail',
'useFileTransport' => false,
'transport' => [
'host' => 'smtp.live.com',
'username' => 'username#live.com',
'password' => 'password',
'port' => '587',
'encryption' => 'tls',
]
],
And here is the actionCreate part of the EmailsController:
public function actionCreate()
{
$model = new Emails();
if ($model->load(Yii::$app->request->post()))
{
$model->attachment= UploadedFile::getInstance($model,'attachment');
if ($model->attachment)
{
$time=time();
$model->attachment->saveAs ('attachments/' .$time.'.' .$model->attachment->extension);
$model->attachment='attachments/'.$time.'.'.$model->attachment->extension;
}
if ($model->attachment)
{
$value= Yii::$app->mailer->compose()
->setFrom (['sharqpress#hotmail.com'=>'Al-Sharq Printing Press'])
->setTo ($model->reciever_email)
->setSubject ($model->subject)
->setHtmlBody ($model->content)
->attach ($model->attachment)
->send();
}
else
{
$value= Yii::$app->mailer->compose()
->setFrom (['sharqpress#hotmail.com'=>'Al-Sharq Printing Press'])
->setTo ($model->reciever_email)
->setSubject ($model->subject)
->setHtmlBody ($model->content)
->send();
}
$model->save();
return $this->redirect(['view','id'=>$model->id]);}
else
return $this -> render('create',['model'=>$model,]);
}
Your configuration needs additional class:
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'viewPath' => '#app/mail',
'useFileTransport' => false,
'transport' => [
'class' => 'Swift_SmtpTransport', // <-- here
'host' => 'smtp.live.com',
'username' => 'username#live.com',
'password' => 'password',
'port' => '587',
'encryption' => 'tls',
]
],

Wordpress SQL Format In theme options

I have come across this code in wordpress theme options table options field:
a:34:{s:15:"generalsettings";N;s:0:"";N;s:12:"color_scheme";s:0:"";s:9:"site_name";s:0:"";s:11:"custom_logo";s:0:"";s:10:"twitter_id";s:48:"also include facebook username:this is a comment";s:13:"header_teaser";s:10:"customtext";s:12:"teaser_style";s:0:"";s:13:"teaser_custom";s:0:"";s:13:"cufon_disable";N;s:18:"breadcrumb_disable";N;s:14:"analytics_code";s:0:"";s:8:"homepage";N;s:11:"teaser_text";s:0:"";s:13:"teaser_button";s:2:"26";s:16:"mainpage_content";s:0:"";s:19:"home_teaser_disable";N;s:14:"slider_disable";s:1:"1";s:12:"slider_speed";s:0:"";s:15:"homepage_slider";s:6:"custom";s:15:"slider_showcats";s:0:"";s:12:"slider_count";s:0:"";s:4:"blog";N;s:9:"blog_page";s:0:"";s:16:"blog_excludecats";s:0:"";s:21:"related_popular_posts";N;s:16:"social_bookmarks";N;s:12:"about_author";N;s:7:"sidebar";N;s:14:"footersettings";N;s:11:"footer_text";s:0:"";s:14:"footer_include";s:0:"";s:11:"navsettings";N;s:12:"show_hide_pg";s:0:"";}
What format is this,and where can i find more information about it?.
Not exactly arrays. they are serialised data. you can use unserialize function to revert it back, and in this example, it is array! =)
Array ( [generalsettings] => [] => [color_scheme] => [site_name] => [custom_logo] => [twitter_id] => also include facebook username:this is a comment [header_teaser] => customtext [teaser_style] => [teaser_custom] => [cufon_disable] => [breadcrumb_disable] => [analytics_code] => [homepage] => [teaser_text] => [teaser_button] => 26 [mainpage_content] => [home_teaser_disable] => [slider_disable] => 1 [slider_speed] => [homepage_slider] => custom [slider_showcats] => [slider_count] => [blog] => [blog_page] => [blog_excludecats] => [related_popular_posts] => [social_bookmarks] => [about_author] => [sidebar] => [footersettings] => [footer_text] => [footer_include] => [navsettings] => [show_hide_pg] => )