Optional Route Parameters

Been tearing my hair out on this one, I’m sure it must be possible, I want to create a rule with optional controller, as described at http://www.yiiframework.com/doc-2.0/guide-runtime-routing.html.

Any help gratefully received on IF it is possible and if it is, how to correctly format my rule.

At URL test.mydomain.com this rule causes an ErrorException - PHP Notice – yii\base\ErrorException

Uninitialized string offset: 48


	'pattern'  => 'http://<clientname:\w+>.mydomain.com/<controller:\w+>',

	'route'    => 'site/index',

	'defaults' => [

		'controller' => 'order',

		'clientname' => 'myclient'



NB. Changing <controller:\w+> to <controller:\w?> produces same error

Cmon, there must be some routing guru out there :)

Put the whole error stack, maybe it can help.

Also, have seen this: https://github.com/yiisoft/yii2/issues/6871 ?

I don’t think you can make the clientname optional as you have done above. If it is missing like:


You will not match http://<clientname:\w+>.mydomain.com/ because the . character after your clientname placeholder will still need to be matched to the URL.

The language example in the guide is similar to what you are doing but the implication is that it must always be present.

It might work if you used http://<clientname:\w+\.>mydomain.com/ but you’d have to test that.