Changing Default Controller

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:

  1. In the view rendering the captcha image, specify the correct route using captchaAction; for example, I use the feedback controller instead of the site controller, so for me it looks like this:

<?php echo $form->field($feedback, ‘captcha’)->widget(Captcha::className(), [

'template' =&gt; '&lt;div&gt;{image}&lt;/div&gt;&lt;div&gt;{input}&lt;/div&gt;',


[color=&quot;#9AAD32&quot;]'captchaAction' =&gt; 'feedback/captcha',[/color]

]) ?>

  1. In the form model (FeedbackForm extends Model in my case), in the rules() specifications, also set captchaAction similar to the following:

public function rules()

{

return [


[['email', 'message'], 'required'],


	[['email'], 'email', 'allowName' =&gt; FALSE],


	[['email'], 'string', 'max' =&gt; 254],


	[['name'], 'string', 'max' =&gt; 100],


	[['message'], 'string', 'max' =&gt; 1000],


	[color=&quot;#9AAD32&quot;][['captcha'], 'captcha', 'captchaAction' =&gt; 'feedback/captcha'],[/color]


];

}