Suffix Per Rule

Hi -

With yii 1.x I was able to assign a different suffix or no suffix at all for each rule.

For example here some of my rules have the .html suffix and some don’t


'urlManager'=>array(

	'urlFormat'=>'path',

	'rules'=>array(

		'status-<train>-train-on-<date>'=>array('status/view', 'urlSuffix'=>'.html', 'caseSensitive'=>false),

		'status-<train>-train'=>array('status/view', 'urlSuffix'=>'.html', 'caseSensitive'=>false),

		'subway/view/<train>'=>'status/view',

		'subway/view/<train>/date/<date>'=>'status/view',

		'<controller:\w+>/<id:\d+>'=>'<controller>/view',

		'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',

		'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',

		),

[...]

With Yii2 I can only add the suffix to the UrlManager array and therefore apply the suffix to all the rules


'urlManager' => ['enablePrettyUrl' => true, 

                  'showScriptName' => false, 'suffix' => '.html',

                  'rules' => ['status-<train>-train-on-<date>' => 'status/view', 

                              'status-<train>-train' => 'status/view', 

                              'subway/view/<train>' => 'status/view', 

                              'subway/view/<train>/date/<date>' => 'status/view', 

                              '<controller:\w+>/<id:\d+>' => '<controller>/view', 

                              '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>', 

                              '<controller:\w+>/<action:\w+>' => '<controller>/<action>', 

                              ], 

                  ],

Any idea on how to achieve the same thing in Yii2?

Thanks

  • Matt

Yes, I got one.

What you haven’t noticed is that any rule can be represented as array, that is,




'status-<train>-train-on-<date>' => 'status/view',

[

	'pattern' => '<some:pattern>',

	'suffix' => '.here-it-is',

	'route' => 'mycontroller/index',

],

Cool, huh?

Got it.

Thx

How it works? We have two patterns and two routes:

Patterns: ‘status-<train>-train-on-<date>’ and ‘<some:pattern>’

Routes: ‘status/view’ and ‘mycontroller/index’

Which will work?

You don’t put the pattern outside the array


 'urlManager' => ['enablePrettyUrl' => true, 'showScriptName' => false,

 'rules' => [ 

				[

				        'pattern' => 'status-<train>-train',

				        'suffix' => '.html',

				        'route' => 'status/view',

				],

				[

				        'pattern' => '<controller:\w+>/<action:\w+>',

				        'route' => '<controller>/<action>',

				]


], ],