-- robert mena <[email protected]> wrote
(on Wednesday, 31 March 2010, 09:16 AM -0430):
> Thanks for the reply.
>
> Ok I know it is incorrect but still there is a problem with the usage - what
> is
> a resource (the method?)?
>
> Ex. My webservice should provide two functions : getPendingRequests() and
> markRequestDone(requestId)
>
> Assuming that the route is Zend_Rest and the controller implements the
> get/post
> /indexAction how would my client make those requests and how would my methods
> *Action be implemented?
>
> Can you put a sample code to illustrate a simple usage?
If you want to expose those two functions using those names, you're
better off with an RPC style service such as XML-RPC or SOAP; those
protocols are more geared towards method calls with arguments.
However, if you want to make it RESTful, you can do so; you'll just
change the paradigm a little.
In this case, your resource would be "request" or "requests" and
correspond to a "RequestController" or "RequestsController".
You would then have implementations for indexAction() and putAction().
indexAction() would correspond to your "getPendingRequests()", and
putAction() would be used for "markRequestDone()". All other methods
would then raise a "403 Forbidden" response header.
As an example:
class RequestController extends Zend_Rest_Controller
{
public function indexAction()
{
$service = new MyWebService();
$view->requests = $service->getPendingRequests();
}
public function putAction()
{
$id = $this->getRequest()->getUserParam('id', false);
if (!$id) {
// no ID found... do something -- raise an error,
// redirect, etc.
}
$service = new MyWebService();
$service->markRequestDone($id);
// view would indicate status
}
public function denied()
{
$this->getResponse()->setHttpResponseCode(403);
$this->render('denied'); // view script indicating
// "forbidden" action
}
public function getAction()
{
$this->denied();
}
public function postAction()
{
$this->denied();
}
public function deleteAction()
{
$this->denied();
}
}
>
> On Wed, Mar 31, 2010 at 8:16 AM, Matthew Weier O'Phinney <[email protected]>
> wrote:
>
> -- robert mena <[email protected]> wrote
> (on Tuesday, 30 March 2010, 03:33 PM -0430):
> > What's the correct way to use Zend_Rest_Server with the MVC (i.e
> > Zend_Controller_Action)?
> >
> > I've read about the Zend_Rest_Route but should I have my Controller
> inherit
> > Zend_Rest_Controller (or define the index/put/get/post/deleteAction)
> and
> put
> > the new Zend_Rest_Server() / handle inside a certain action?
>
> NO!!!!
>
> Zend_Rest_Route/Controller are the first step in _deprecating_
> Zend_Rest_Server. If you look carefully at Zend_Rest_Server, it's not at
> all RESTful. Zend_Rest_Route/Controller introduce a RESTful paradigm to
> developing applications in ZF, using the MVC.
>
> All you need to do is use the Rest route, and have controllers that
> either extend the Rest controller or implement the necessary methods.
>
> * "indexAction" should return lists of resources, and respond only to
> GET requests (this is already enforced by the Rest route)
>
> * "getAction" should return the requested resource (indicated by the
> "id" request parameter, which is part of the uri: "/foo/32" -> id ==
> 32)
>
> * "postAction" should create a new resource based on the content posted
> to it, and then return a representation of it while simultaneously
> indicating the new resource's canonical location via a Location
> header and 201 response code.
>
> * "putAction" should update a specific resource as indicated by the
> "id" request parameter (again, part of the URI), and then return a
> representation of the updated item. Typically, it should only respond
> to PUT requests, but the route also supports POST requests with the
> query parameter "_method" set to "put".
>
> * "deleteAction" should delete the given resource as indicated by the
> "id" request parameter (again, part of the URI), and return a 204
> response code with no content on success. Typically, this request
> should be via an HTTP DELETE, but can also be via a POST request with
> a query parameter "_method" set to "delete".
>
> How you do the representations and submissions is up to you -- it can be
> HTML, XML, JSON -- that will simply depend on your application needs.
>
> --
> Matthew Weier O'Phinney
> Project Lead | [email protected]
> Zend Framework | http://framework.zend.com/
> PGP key: http://framework.zend.com/zf-matthew-pgp-key.asc
>
>
--
Matthew Weier O'Phinney
Project Lead | [email protected]
Zend Framework | http://framework.zend.com/
PGP key: http://framework.zend.com/zf-matthew-pgp-key.asc