set Theme not just request wise

Hello,

i have the problem, that i wanna set a Theme using the provided setTheme method from the Application Component. Works pretty good, but the theme is set for one request only.

Is there any way to set a theme permanently?

Or any application wide event i can use to set the theme for every request. I think i could use the beforeRequest Method of the application component, but don't know how to register an eventlistener.

Any help would be really appreciated.

Cheers Christian

This is a little example, on how to change the theme of your application, if requested from an iPhone:

protected/config/main.php



	// behaviors


	'behaviors' => array(


		'onbeginRequest' => array(


			'class' => 'application.components.Startupbehavior',


		),


	),


protected/components/Startupbehavior.php



<?php


class Startupbehavior extends Cbehavior


{


	public function attach($owner)


	{


		// set the event callback


		$owner->attachEventHandler('onbeginRequest', array($this, 'beginRequest'));


	}





	/**


	 * This method is attached to the 'onbeginRequest' event above.


	 **/


	public function beginRequest(CEvent $event)


	{


		if(stripos(CHttpRequest::getUserAgent(),'iPhone'))


			Yii::app()->theme='iphone';


	}


}


Hope that works for you. :)

Great. Works as expected. Although I find setting the theme on each request is a bit of a hassle.

Thanks

You guys getting the following errors when applying the excellent code above?


PHP Error

Description


include(Cbehavior.php) [<a href='function.include'>function.include</a>]: failed to open stream: No such file or directory

Source File


C:\XAMPP\xampp\yii\framework\YiiBase.php(338)



Solution below;

Note ‘Cbehavior’ must be ‘CBehavior’!! I’ve add *'s to keep the cap B. The forum drops the case of ‘B’ to ‘b’ and breaks the above code. Took me a few hours to realise this ‘gotcha’!!

Where I found evidence for my issues…

http://www.yiiframework.com/forum/index.php?/topic/4368-problem-with-camelcase-in-forum/page__hl__Cbehavior__fromsearch__1