Yii Framework Forum: Yii Registry - Yii Framework Forum

Jump to content

  • (2 Pages)
  • +
  • 1
  • 2
  • You cannot start a new topic
  • This topic is locked

Yii Registry Rate Topic: ***** 1 Votes

#1 User is offline   Vince. 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 432
  • Joined: 29-December 08
  • Location:Los Angeles,CA

Posted 30 July 2009 - 03:25 AM

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.
Vince Gabriel, Lead Developer.
Posted ImagePosted ImagePosted Image
0

#2 User is offline   krak3n 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 81
  • Joined: 17-February 09
  • Location:London, United Kingdom

Posted 30 July 2009 - 04:29 AM

View PostVince., on 30 July 2009 - 03:25 AM, said:

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

#3 User is offline   Vince. 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 432
  • Joined: 29-December 08
  • Location:Los Angeles,CA

Posted 30 July 2009 - 04:36 AM

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.
Vince Gabriel, Lead Developer.
Posted ImagePosted ImagePosted Image
0

#4 User is offline   mbi 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 604
  • Joined: 08-May 09

Posted 30 July 2009 - 04:44 AM

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
0

#5 User is offline   Vince. 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 432
  • Joined: 29-December 08
  • Location:Los Angeles,CA

Posted 30 July 2009 - 04:51 AM

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.
Vince Gabriel, Lead Developer.
Posted ImagePosted ImagePosted Image
0

#6 User is offline   mbi 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 604
  • Joined: 08-May 09

Posted 30 July 2009 - 05:04 AM

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

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

0

#7 User is offline   Vince. 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 432
  • Joined: 29-December 08
  • Location:Los Angeles,CA

Posted 30 July 2009 - 05:22 AM

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;
Vince Gabriel, Lead Developer.
Posted ImagePosted ImagePosted Image
0

#8 User is offline   MarcS 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 96
  • Joined: 05-July 09

Posted 30 July 2009 - 08:40 AM

View PostVince., on 30 July 2009 - 05:22 AM, said:

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.yiiframew...ension/dbparam/
0

#9 User is offline   Vince. 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 432
  • Joined: 29-December 08
  • Location:Los Angeles,CA

Posted 30 July 2009 - 08:58 AM

That extension uses the DB, I don't think it's a good way to handle that.
Vince Gabriel, Lead Developer.
Posted ImagePosted ImagePosted Image
0

#10 User is offline   MarcS 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 96
  • Joined: 05-July 09

Posted 30 July 2009 - 11:15 AM

View PostVince., on 30 July 2009 - 08:58 AM, said:

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

#11 User is offline   Vince. 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 432
  • Joined: 29-December 08
  • Location:Los Angeles,CA

Posted 30 July 2009 - 11:30 AM

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
Vince Gabriel, Lead Developer.
Posted ImagePosted ImagePosted Image
0

#12 User is offline   qiang 

  • Yii Project Lead
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,871
  • Joined: 04-October 08
  • Location:DC, USA

Posted 30 July 2009 - 01:32 PM

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

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

#13 User is offline   Vince. 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 432
  • Joined: 29-December 08
  • Location:Los Angeles,CA

Posted 30 July 2009 - 01:41 PM

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.
Vince Gabriel, Lead Developer.
Posted ImagePosted ImagePosted Image
0

#14 User is offline   qiang 

  • Yii Project Lead
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,871
  • Joined: 04-October 08
  • Location:DC, USA

Posted 30 July 2009 - 01:44 PM

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

#15 User is offline   auxbuss 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 164
  • Joined: 14-July 09
  • Location:Malvern, UK

Posted 30 July 2009 - 02:25 PM

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

#16 User is offline   Vince. 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 432
  • Joined: 29-December 08
  • Location:Los Angeles,CA

Posted 31 July 2009 - 01:40 AM

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.
Vince Gabriel, Lead Developer.
Posted ImagePosted ImagePosted Image
0

#17 User is offline   Lucas 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 20
  • Joined: 04-June 09

Posted 31 July 2009 - 02:31 AM

View PostVince., on 31 July 2009 - 01:40 AM, said:

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

#18 User is offline   abajja 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 108
  • Joined: 05-March 09
  • Location:Barcelona, Spain

Posted 31 July 2009 - 03:54 AM

Quote

I would like to be able to use Registry Pattern in Yii

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

#19 User is offline   auxbuss 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 164
  • Joined: 14-July 09
  • Location:Malvern, UK

Posted 31 July 2009 - 05:05 PM

View PostVince., on 31 July 2009 - 01:40 AM, said:

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.

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

#20 User is offline   knut 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 45
  • Joined: 10-October 08
  • Location:Notodden, NORWAY

Posted 31 July 2009 - 05:20 PM

What's wrong with:

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

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


?
0

Share this topic:


  • (2 Pages)
  • +
  • 1
  • 2
  • You cannot start a new topic
  • This topic is locked

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