urlManager rules in module

Is it possible to set urlManager rules from a custom module?

If it's not in the core classes, what is the best way to implement it?

I would like it, too.

Actually I implement this by controlling it in every controller on the module. Not the best approach, I guess.

Unfortunately it is too late to set url rules inside a module because by that time, the request is already resolved.

A workaround to this problem is to collect URL rules actively from various modules. In particular, you can write app config like following:



'urlManager'=>array(


    'rules'=>require('rules.php'),


),


Inside rules.php, you can search for rules that are provided by installed modules. Assuming you store rules for module x in the file protected/modules/x/config/rules.php, then the main rules.php can contain the following code:



array_merge(


   require('path/to/x/rules.php'),


   require('path/to/y/rules.php'),


  .....


Thanks.

qiang, thanks for the tip! Just what I was looking for.

You can also just put the array_merge() in the main.php config file. That way you don’t have to create an additional application-level configuration file that just does nothing but perform the array_merge(). Something like this:




	'components'=>array(

		...

		'urlManager'=>array(

			'urlFormat'=>'path',

			'showScriptName' => false,

			'rules'=>array_merge(

				array(

					// Your main rules will go here

					...

				),

				// Your module rules will go in here

				require_once('protected/modules/x/config/url-rules.php'),

				require_once('protected/modules/y/config/url-rules.php'),

				...

			),

		),

		...



Then in your protected/modules/x/config/url-rules.php you put:




<?php

	return array(

		'/custom/path1' => 'x/controller1/action1',

		'/custom/path2' => 'x/controller1/action2',

		'/custom/path3' => 'x/controller2/action1',

		'/custom/path4' => 'x/controller2/action2',

		...

	);



It would be great if eventually Yii had a better built-in mechanism for doing this. It really makes sense to allow modules direct access to the application’s urlManager.