Hi guys,
How can changing default controller in yii2?
there is no defaultController in app config
Thanks
Hi guys,
How can changing default controller in yii2?
there is no defaultController in app config
Thanks
It is not default controller anymore, it is defaultRoute:
http://stuff.cebe.cc/yii2docs/yii_web_application.html#$defaultRoute-detail
I changed it to something else (PageController).
when I tried to browse /page/contact I got error:
Invalid CAPTCHA action ID: site/captcha
P.S:
Also I changed SiteController to PageController
You need to set $captchaAction param for your captcha widget. Default is site/captcha, which does not exist anymore (coz you changed it)
I know but how I can do it in controller?
This widget is in view views/page/contact.php
Seems like validator config param (captchaAction) must also be set
https://github.com/yiisoft/yii2/blob/master/docs/guide/validation.md#captcha-captchavalidator
I know what should I do but I don’t know how.
I must set CaptchaValidator->$captchaAction in controller? how?
The application templates as well as the generated apps create a class called SiteController extended from Controller. You could have used simple inheiritance to avoid this situation.
class PageController extends SiteController
{
//add your own code here
}
PageController will inheirit the dependencies and actions of SiteController as well as Controller. In this particular case, LoginForm.php is a SiteController dependency. You can of course override the SiteController and Controller methods in PageController if you choose to do so.
The parent class, Controller, is not an abstract class but is made abstract because it implements from ViewContextInterface which establishes the mandatory methods that must exist through inheritance in the class hierarchy.
If and when you create modules, you will note that the gii generated module controllers are inheirited directly from Controller. You could use inheiritance in a similar manner to create your own ModuleController class extended from Controller and extend your module controllers from that class rather than Controller allowing you to override Controller methods as you require them and to also add common code you want to share among all of your modules.
In my experience, also from changing the default controller and removing the site controller, I had to do two things to make it work:
<?php echo $form->field($feedback, ‘captcha’)->widget(Captcha::className(), [
'template' => '<div>{image}</div><div>{input}</div>',
[color="#9AAD32"]'captchaAction' => 'feedback/captcha',[/color]
]) ?>
public function rules()
{
return [
[['email', 'message'], 'required'],
[['email'], 'email', 'allowName' => FALSE],
[['email'], 'string', 'max' => 254],
[['name'], 'string', 'max' => 100],
[['message'], 'string', 'max' => 1000],
[color="#9AAD32"][['captcha'], 'captcha', 'captchaAction' => 'feedback/captcha'],[/color]
];
}