How to configure a default controller of a module in Yii2


Actually I just started Yii2. I just want to know how can I configure a default controller of a module in it.

Is it similar to Yii1 ??

Any help will be appreciated.

Yes, it’s similar. Check API docs for Module.

Yea I found the answer. But a slight difference in it.

In Yii2 we are using $defaultRoute for this configuration. But in Yii1 it was $defaultController.

So we can configure it in Module same as Yii1 like :

public $defaultRoute		= '<your-controller-name>';

Yeah. Name is different but the idea is exactly the same.

As an additional note when we define default controller for a module if we have improper routing rules we will get 404. There are some sensitive notes about routing patterns , for example :

'urlManager' => [

	'enablePrettyUrl' => true,

	'showScriptName' => false,

	'enableStrictParsing' => true,

//	'caseSensitive' => false, seemingly is not applicable in yii2

	'rules' => [

/*		'admin/<_controller:\w*>/<_action:\w*>' => 'Admin/<_controller>/<_action>',*/

			//"admin" with or without a following slash will make a 404 but "admin/default" will be OK 200

/*		'admin/<_controller:\w*>' => 'Admin/default/index', */

			//"admin/" will be 200 OK but "admin" will be 404. I made the right side intentionally static addressing to clear the note about the pattern.

/*		'admin<_controller:(\/|\/\w+)?>' => '...', */

			//this will be 200 OK for both "admin" and "admin/" and "admin/controller" BUT "admin/controller/" will be 404

		'admin<_controller:(\/|\/\w+)?><_action:(\/|\/\w+\/?)?>' => 'Admin<_controller><_action>',

			//this will make slashes, controller and index name arbitrary



Edit :

This way the naming of $defaultRoute instead of $defaultController is rational hierarchically in the sys of apps -> mods.