zend framework 2 - use global method - zend-framework

i need one function which i use in views, hydrator, some controllers and so on...
Where i can put it?
Where it would be the best add that can be maintained PHP OOP and zend 2 architecture?
Thanks

It sounds like you should be looking at the Zend\ServiceManager. You can register factories (functions) and services in the service manager, and technically access them from anywhere in your application. Your class would need to implement the ServiceLocatorAwareInterface in order to access the service manager, OR you would pass/inject the service into your class/model/hydrator/etc.

Related

Perl Catalyst: How to re-use applications

I did implement a Catalyst authentication application (captchas, password reminders, access logs, etc...).
How am I supposed to re-use it in different Catalyst applications? I.e.:
Or - more generally - how am I supposed to let two applications talk each other?
You're basically asking : I have build two airplanes, how do I let them talk to each other?
Maybe see Catalyst::Plugin::Authentication (and module that use it) and make one yourself,
or something else entirely, like a radio
You can abstract common components in your local catalystX namespace and extend your controllers and models from that namespace .

CAB: Get service instance without having a reference to WorkItem

Is it possible to get an instance of a service without having a WorkItem context?
I have a some classes that need to access some services, and i'm wondering if it's possible to get those services without explicitly injecting those services in the class.
As all the services are registered in WorkItem or rootWorkItem context its not possible according to the design rules of CAB/SCSF.
Please elaborate why you cannot register the service in WorkItem and get it from there. CAB/SCSF has proposed the best practices to manage an enterprise application, its upto us how much we benefit from it.
But if its really necessary you can have a static class (which can act as service implemented in singleton way) in Infrastructure.Library and refer this assembly in your Business or Functional module to get it.
Its a bad hack but technically doable.

Where to put Service/Data Access Classes in a Zend Framework App

I originally wanted to find out how to access the Doctrine 2's Entity Manager from within Entity Classes. But I saw another question Using EntityManager inside Doctrine 2.0 entities, and learnt that I should be using a service class. I wonder where should I put in a Zend Framework Application? Also is it also called a DAO (Data Access Object)? I am thinking of naming it DAO instead of Service as Service sounds alot like something for external sites to use (like Web Service)?
I am thinking something like Application_Models_DAO_User?
Service classes are part of the autoloader mapping. Like Application_Model_Something can be found in application/models, it's the same for services.
An application service Application_Service_Something should be located at: application/services/Something.php
When you use service classes inside modules, for example Blog_Service_Something they need to be located at: application/modules/blog/services/Something.php
I think classes like entity managers shouldn't be part of your controllers nor your models, but located in service classes.

Where to set the ModelMetadataprovider with DI when using MVC Turbine?

To use your own ModelMetadataProvider you normally set it in the global.asax.
I'm using MVC Turbine and I need to inject a dependency into my ModelMetadataProvider as well.
Something like this:
ModelMetadataProviders.Current = new MyModelMetadataProvider(ISomeDependency);
How is this best accomplished with MVC Turbine?
The best place to put these pieces is to override the Startup method within your web application (the type that inherits from TurbineApplication). We're currently working on making these MVC2 features easier in v2.2 by introducing a ModelMetadataBlade that will do all the wiring up for you to the ModelMetadataProvider.Current property.
So all you'll have to do is register MyModelMetadataProvider with the container like so
container.Register<ModelMetadataProvider, MyModelMetadataProvider>()
then MVC Turbine will do the rest for you. To get an idea of what I'm talking about, checkout the way we're wiring up ModelValidatorProviders. The ModelValidatorBlade asks the ServiceLocator for all the registered ModelValidatorProvider and wires them up with the runtime.
If you have any feedback or ideas, could you post them to the Google Group? Trying to keep these things organized :)
Thanks!

Implementation differences between Zend_Rest_Server & Zend_Rest_Controller

There seems to be two different ways in the Zend Framework to implement a RESTful API, one by adding objects&functions to the Zend Rest Server object inside a action controller, the other through extending the very sparsely documented Zend Rest Controller method mentioned in the Zend Rest Router configuration.
My questions are:
Do you configure the Router to point at the Zend_Rest server or
Do you extend the Zend_Rest_Controller class and instantiate business objects inside action methods?
Thanks!
You should use Zend_Rest_Route and extend Zend_Rest_Controller.
Zend_Rest is far from beeind RESTfull its more of another RPC.