Zend Custom Business Class - zend-framework

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.

Related

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.

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

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

init method in zf2 controller

In Zendframework 1 we use init() method for initialize stuff in controller. I saw that this is taken out from zenframework 2. Why? and what is the best way to achieve same thing in zf 2. I am upgrading my previous project developed in zf1 and I can see things has changed a lot in zf2 as compare to zf1.
Is there anyother change in zf2, they way we use other methods such as preDispatch() and postDispatch() in zf1?
Anyone has gone through this?
In zf2 controllers are instantated by the ControllerLoader, which is a subclass of the ServiceManager. If you need to initalize a controller, either use a Factory, or __construct. Use __construct for simpile initalizations, and use a Factory if the controller consumes other objects that need to be injected.
preDispatch and postDispatch are also gone in favour of the new events system. To get the same result in zf2, register event handlers for the disptach and render events. For a full list of mvc envents see http://akrabat.com/zend-framework-2/a-list-of-zf2-events/
Also, take a look here for an example of setting up a controller factory ZF2 how to get entity Manager from outside of controller
I think you can drop this into a controller and it will work.
public function onDispatch(MvcEvent $e)
Since OP mentions postDispatch, it's worth noting that __destruct now works in a similar manner. One big difference, though, is that execution cannot be prevented (e.g. through exit;) turing tear-down of the Object.

MEF and MVC - a few pointers please :)

What I am trying to do is inject a component into my MVC app and make use of it from the controllers.
Currently I am not trying to use MEF for the actual controllers, but i want to import components e.g. A loggin component into the MVC app.
Where is the best place to do this?
Currently I have, directly in the controller, put my compose parts code and ILogger property, but I get the feeling this is not the best way. Seems like I should only need to call Compose once in the application.
So should it be in the global asax file that I do the compose?
If so, how do I get a handle on ILogger from my controllers? Should I have a "base" controller, where i inject ILogger into the constructor and inherit every standard controller from?
Hope that makes sense - I'm just struggling a bit with the structure of my code.
Thx
I use Log4Net and inject the logger into each controller. I dont think its a big hit when you use injection. Take a look at Ninject. It has both an MVC implementation and a logging module. The modules are loaded once in the global, then it injects the controllers. Basic DI, but do you really need more? If you create a base controller you will still have to create a ctor in each controller that can be injected.
You might create a base controller with the logging, then use property injection. I have never done this, but if all controllers use the same base it should work fine.

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.