Yii Registry
#1
Posted 30 July 2009 - 03:25 AM
Thanks.
#2
Posted 30 July 2009 - 04:29 AM
Vince., on 30 July 2009 - 03:25 AM, said:
Thanks.
You could make a base controller that all controllers extend from and declare any arrays, objects or functions in there you may need throughout the application.
#3
Posted 30 July 2009 - 04:36 AM
#4
Posted 30 July 2009 - 04:44 AM
Yii::app()->getParams()->firstname = "marko"; var_dump(Yii::app()->getParams()->firstname);
getParams() returns a CAttributeCollection, which is a CMap, which is an associative array in OO-style
#5
Posted 30 July 2009 - 04:51 AM
#6
Posted 30 July 2009 - 05:04 AM
Yii::app()->registry->firstname = "marko"; echo Yii::app()->registry->firstname;
#7
Posted 30 July 2009 - 05:22 AM
Currently i just do
Yii::app()->setParams(array('key' => 'value'));
then i just access it using Yii::app()->getParams()->key;
#8
Posted 30 July 2009 - 08:40 AM
Vince., on 30 July 2009 - 05:22 AM, said:
Currently i just do
Yii::app()->setParams(array('key' => 'value'));
then i just access it using Yii::app()->getParams()->key;
Check out this extension
http://www.yiiframew...ension/dbparam/
#9
Posted 30 July 2009 - 08:58 AM
#10
Posted 30 July 2009 - 11:15 AM
Vince., on 30 July 2009 - 08:58 AM, said:
You could just slightly modify it to not use the db - if you don't need to persist your values
Looking at the code I think it shouldn't be a big deal
Just write to an array instead to the database
You could also extract CakePHP's Configure class from cakePHP and use that. Really shouldn't be very hard to do.
#11
Posted 30 July 2009 - 11:30 AM
#12
Posted 30 July 2009 - 01:32 PM
Yii::app()->params[key]
or
Yii::app()->params->key
#13
Posted 30 July 2009 - 01:41 PM
#14
Posted 30 July 2009 - 01:44 PM
#15
Posted 30 July 2009 - 02:25 PM
Almost always, a piece of "global" data has a natural object that it can live with. For example, a web-site visitor/user, whose data you may persist in a session variable -- as an object, of course -- between requests. Or a shopping cart, say, although this is just part of the user object.
The classic case that I've seen numerous times, is where a web-site supports multiple countries/languages and the code is riddled with if-country1-do_this-else-if-country2... creating a shotgun surgery exercise every for every change and making the whole thing unscaleable. That's what polymorphism is for!
I'm kind of intrigued that I see folk asking for something that I regard as odd, and something I don't see elsewhere in an oo environment.
I'm would like to better understand. I'm not judging.
#16
Posted 31 July 2009 - 01:40 AM
#17
Posted 31 July 2009 - 02:31 AM
Vince., on 31 July 2009 - 01:40 AM, said:
I think he understood you just fine, he's just wondering why you need the registry pattern, as in essence, it's just a nicer looking way of adding objects to the global scope
#18
Posted 31 July 2009 - 03:54 AM
Quote
... A final class with mutators !!. I'd prefer the declarative way of Yii. Just an opinion.
#19
Posted 31 July 2009 - 05:05 PM
Vince., on 31 July 2009 - 01:40 AM, said:
Thanks, Vince. That explains a few things.
What I see in the "registry pattern" that you kindly linked to is a singleton that simply provides a global associative array (i.e. a map). Yii already provides the equivalent where it's appropriate -- that is, not to break MVC -- so I'm going to persevere and ask you for more specific details about why you feel that you can't do what you want to Yii. What is it that you can't get from one location to another without the need for a global variable? And what are those locations?
#20
Posted 31 July 2009 - 05:20 PM
Yii::app()->params['foo'] = 'bar';
and:
echo Yii::app()->params['foo'];
?

Help

This topic is locked















