Implementation differences between Zend_Rest_Server & Zend_Rest_Controller - zend-framework

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.

Related

JAX-RS Struts2 REST API

Why would one like to integrate JAX-RS(Jersey) using Rest API to Struts2? Struts2 is itself a mvc framework, so why would anyone want to integrate these both? If combined, how will the resulting framework be(I wanted to know if REST API just control the controller part of MVC).
There is a RESTful plugin called struts2-rest-plugin that has been included with the framework since version 2.1.1. A fair amount of information on the plugin can be found here.
Essentially, the plugin uses a custom action mapper that examines the request and based upon the HTTP Method used in conjunction with the URI, it dispatches the request to one of several different method names (e.g. GET /movies dispatched to index() method of action).
Just because Struts2 is an action-based framework does not mean a RESTful solution cannot be included as an alternative for developers. Spring MVC offers similar solutions themselves and it is also an action-based framework.
If you consider your JSON response as your view, you'll see that the fact that Struts2 is based on MVC design makes logical sense. Your model is simply the data structure you are returning to the client and your controller is the action.
Consider reading the link above on the plugin and you'll get a better picture of how the two can be integrated. If you want to return JSON but don't necessarily want to offer RESTful URLs in your Struts2 application, you can also consider the JSON plugin, found here.
I am not sure about Struts2, but in the past Struts1 did not have a "Rest" adapter built in. Jersey provides the cool #annotations that will easily serialize your datamodel and will push you in a "Restful" direction. Jersey does not provide an MVC framework as much as it provides convenience methods to work in a Restful/resource based way.

laravel 4 restful controllers manually

In the laravel docs they have explained using resourceful controllers for using REST and using verbs in the methods of the controllers.
I have manually listed all my routes like this:
Route::get('users/{id}/friends', 'UsersController#friends');
I am not using any resource just the method and the uri.
How can I make this restful? Or is it restful already? Then how do I call the methods in a RESTful manner? I am confused
REST is nothing more than a convention on how data should transfer in the web. If you want to be all nitpicky about it check this out: http://en.wikipedia.org/wiki/Representational_state_transfer#Architectural_constraints
As for laravel, when you generate a controller via the CLI like so:
php artisan controller:make UsersController
Laravel automatically creates the boiler plate for the various actions you need to be handling (create, store, destroy, update and so on.) and then you can set-up your routes. Of course you wont need all for most controllers.
Here's a helpful set of links from Phillip Brown's blog:
http://culttt.com/2013/07/01/setting-up-your-first-laravel-4-controller/
http://culttt.com/2013/08/12/building-out-restful-controller-methods-in-laravel-4/
If you want to make it RESTful, the simplest way in laravel is to do Route::resource('user', 'UserController), which automatically gives you the routes shown on the laravel web page.
However, if you are trying to look up the friends RESTfully you would want to make a /friends/ resource, and then register it as Route::resource('friends', 'FriendController'). For more on the relationships/friends side of things I would suggest reading this question from 2011, while not laravel specific, it does answer questions about REST.

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

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.