Yii Registry

Does Yii has some sort of a registry implementation? Or a config object? something we can store arrays/objects and access them any where.

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.

That’s exactly what i do now. That’s not enough though, I use helpers, widgets, plugins, API etc… i can access the Yii::app() in all of them but the $this is registered in just some of them. There must be a way to register items into the Yii::app() object, Like Zend_Registry. I could store anything i want in the Zend_Register by doing Zend_Register::set(‘DB’, $dbobject); then accessing it anywhere i want by doing $db = Zend_Register::get(‘DB’); even out side the scope of the framework.

What about this?




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

Yea the params could work, But it’s a shame i can’t just use the Yii::app()->value like that, It’s shorter and more straight forward.

You could create your own registry component, so you access it like this:




Yii::app()->registry->firstname = "marko";

echo Yii::app()->registry->firstname;



Ye i know that, But Yii should implement a registry for easier access.

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.yiiframework.com/extension/dbparam/

That extension uses the DB, I don’t think it’s a good way to handle that.

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.

I just did Yii::app()->setParams(array(‘global’ => &this)) in my main class where i do everything. Then i can access the global param anywhere

Yii::app()->getParams()->key can be written in shorter form:

Yii::app()->params[key]

or

Yii::app()->params->key

I tried doing Yii::app()->params->key and i thought it will use PHPs magic method but it thrown an error saying the that property doesn’t exists.

Are you sure key exists? This should work (we have unit tests covering it).

Vince, can you tell me what kind of information you need globally accessible. There’s a similar thread running where I asked the same question. Globals are tight coupling and can make testing tricky, so it’s generally a “bad thing”. But there are very good examples where it’s necessary. Heck, a database is global!

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.

I think you just misunderstood me (or us), I would like to be able to use Registry Pattern in Yii, As i could use in Zend with it’s Zend_Registry object.

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

… A final class with mutators !!. I’d prefer the declarative way of Yii. Just an opinion.

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?

What’s wrong with:




Yii::app()->params['foo'] = 'bar';



and:




echo Yii::app()->params['foo'];



?