Yii Framework Forum: set configuration dynamically - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

set configuration dynamically trying to set urlManager rules at runtime with setComponents Rate Topic: -----

#1 User is offline   soeperees 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 10
  • Joined: 26-October 09
  • Location:The Hague, The Netherlands

Posted 13 May 2010 - 03:20 AM

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!
0

#2 User is offline   zaccaria 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 2,232
  • Joined: 04-October 09
  • Location:Moscow

Posted 13 May 2010 - 04:57 AM

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)
0

#3 User is offline   soeperees 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 10
  • Joined: 26-October 09
  • Location:The Hague, The Netherlands

Posted 13 May 2010 - 05:40 AM

hi zaccaria,
Thanks for your reply.

Quote

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

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?
0

#4 User is offline   andy_s 

  • Random Member Title
  • Yii
  • Group: Moderators
  • Posts: 1,526
  • Joined: 22-June 09
  • Location:Russia, Kostroma

Posted 13 May 2010 - 06:39 AM

http://www.yiiframew...-manager-routes
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users