Using the ServiceLocator in ZF2 with Doctrine 2 from/in a Custom Class? - frameworks

i have a little problem using doctrine 2 in Zend Framework 2 i have custome class that i use to manipulate doctrine generated model (basically to inject data and populate), to make that work i need the entity manager which is available through the service manager as indicated in Jason Grimes tutorial here http://www.jasongrimes.org/2012/01/using-doctrine-2-in-zend-framework-2/.
In his tutorial it works (i tested it) as the ServiceLocator is called from a controller class, but for the application i am writing i have to use custom (non controller) classes to interact with the entities. How do i achieve this? Using the servicelocator from a custom class that is not a controller? Thank you all in advance

You need to do two steps
Implement Zend\ServiceManager\ServiceLocatorAwareInterface in your custom class. This allows to the Framework to inject the Service Locator for you.
Convert your custom class to a service and retrieve it using Service Manager. This component will check if the class implement ServiceLocatorAwareInterface and the inject the ServiceLocator before returning to you the instance

Related

Resolve binding without instatiating object in Ninject

I have interface (e.g. IMyInterface). I need to get of which type object will be created if I call kernel.Get<IMyInterface>(). Not the instance of IMyInterface but the type of instance without creating instance itself. Is it possible?
Ninject doesn't have this functionality. From a Ninject IKernel (like StandardKernel) you can call GetBindings(Type) but Ninject uses a provider model, so the binding's provider has to be invoked to view the result.
I would suggest creating a custom meta-service to mappings of services to implementations, if this functionality really needs to be dynamic.

zend framework 2 - use global method

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.

Zend Custom Business Class

I have a membership class I want to use throughout my website. I was wondering what the easiest way would be to load it so I can access it in my Models and Controllers?
Would I need to use Zend Autoload? Also is there a convention for where to put it? Like in /application/business/membership.php?
What about third party classes?
You would turn it into a helper class.
This can either be a standalone class, or it could be an action helper which you would register with the Action Helper Broker.
http://framework.zend.com/manual/en/zend.controller.actionhelpers.html
Zend_Controller_Action_HelperBroker::addHelper($helper);
Where $helper is the instantiate class.

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.

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.