set configuration dynamically

Hi all,

I’m trying to set urlManager rules at runtime with the following code in my controller:




$components=array(

		'urlManager'=>array(

			'urlFormat'=>'path',

			'showScriptName' => false,

			'rules'=>array(

				'/contact'=>'/site/contact',

			),

		),

	);

Yii::app()->setComponents($components);



Somehow this doesn’t work. What am I doing wrong?

Is there another way to set configuration at runtime?

Thanks in advance!

The problem can be that once you set this options, urlManager has already done his job, and so is too late.

2 advicies:

1 I really cannot immagine why you want to change urlManage in a controller, but that’s not my problem… :)

2 you can create a behaviour for your application, and in this behaviour create an handler for the onBeginRequest




	public function events()

	{

		return array_merge(parent::events(), array(

			'onBeginRequest'=>'beginRequest',

		));

	}


	/**

	 * Load configuration that cannot be put in config/main

	 */

	public function beginRequest()

	{

		// set the user language, for example

		


	}



and in config.main you can attach this behaviour to the application.

Like that the configuration will be used for anithing that heppens after onBeginRequest (see application lifecycle)

hi zaccaria,

Thanks for your reply.

You are right. I tried that just for testing.

The point is that I’m trying to build a CMS. When someone creates a new page I need to set a new rule for urlManager.

basically i want to get those rules from the database.

I’m having a hard time figuring out what the right place is to do things like this. Do I have to create an application component and override a system class?

http://www.yiiframework.com/forum/index.php?/topic/5178-dynamic-url-manager-routes