Re: [fw-general] Zend_Rest_Controller - n00b question

2009-09-07 Thread Matthew Weier O'Phinney
-- Edward Haber  wrote
(on Monday, 07 September 2009, 02:29 PM -0400):
> Doesn't this component render Zend_Rest_Server moot? I'm sure I'm  
> missing something, but Zend_Rest_Controller seems to provide a much  
> better interface to managing REST contexts than Zend_Rest_Server as ZRC 
> makes the route->to->context translation invisible. What did I miss?

You're not missing anything; we plan to deprecate Zend_Rest_Server. :)

Zend_Rest_Server is actually not a RESTful implementation -- it's more
of an RPC implementation, and XML-RPC is a better choice in such a
situation. Zend_Rest_Route provides the means to create RESTful services
using the MVC layer -- which is arguably better, as REST is very
url-centric, which is what the router and MVC provide infrastructure
for.


> On Sep 7, 2009, at 11:11 AM, Matthew Weier O'Phinney wrote:
>
>> -- DorkFest  wrote
>> (on Monday, 07 September 2009, 07:28 AM -0700):
>>> Is there any documentation on this component?
>>>
>>> Can this controller be used to build an XML/JSON based API service?  
>>> If so,
>>> is there a built in method for retrieving an XML request fragment to 
>>> the
>>> controller (ie: not key/val pairs)? I think this is normally access  
>>> with
>>> something like: file_get_contents() from stdin. Wondering if ZF has a
>>> built-in way.
>>
>> You can pull in the raw request body from the request object:
>>
>>   $body = $this->getRequest()->getRawBody();
>>
>> With JSON data, it's trivial then to get your data:
>>
>>   $data = Zend_Json::decode($body);
>>
>> With XML, simply pass it to SimpleXML:
>>
>>   $data = new SimpleXMLElement($body);
>>
>> What I've done is to create an action helper that checks the "Accept"
>> request header ($request->getHeader('Accept')), and then sets the
>> "format" request parameter accordingly:
>>
>>   // Accept headers often are of format: "application/json;  
>> charset=utf-8"
>>   $accept = $request->getHeader('Accept');
>>   $accept = explode(';', $accept);
>>   $accept = trim(array_shift($accept));
>>
>>   switch ($accept) {
>>   case "application/json":
>>   $request->setParam('format', 'json');
>>   break;
>>   case "application/xml":
>>   $request->setParam('format', 'xml');
>>   break;
>>   }
>>
>> Put the above code in the preDispatch() method of your action helper,
>> and then register that helper with the helper broker in your bootstrap
>> to ensure it's run for each controller requested.
>>
>> Then, in your controller, you can initialize the ContextSwitch helper:
>>
>>   $context = $this->_helper->contextSwitch;
>>   $context->setAutoJsonSerialization(false); // do this so you can  
>> have
>>  // JSON-specific views
>>   $context->addActionContext('index', 'xml')
>>   ->addActionContext('index', 'json')
>>   // ...
>>
>>   $context->init();
>>
>> If the context (xml or json, in the example above) is found (which  
>> will
>> be based on the "format" set previously), then it will attempt to  
>> render
>> a different view script: index.xml.phtml or index.json.phtml,
>> respectively. This allows you to keep your controller lean, and have
>> view-specific logic for each format. (For instance, I like to set my
>> response headers, such as Content-Type, Content-Range, etc., within my
>> views.)
>>
>>> I'm surprised there are very few posts and almost no documentation.  
>>> Does
>>> anyone have info on this component?
>>
>> Well, the documentation is primarily centered around the basic usage  
>> of
>> the component, which is for creating RESTful resources in the MVC via a
>> combination of routing and specific actions in your controller. How  
>> you
>> handle different content types is something else entirely. I'll be
>> blogging on some of this information in the coming months, and also
>> covering it during my "Architecting Ajax Applications in Zend  
>> Framework"
>> tutorial at ZendCon.
>>
>> -- 
>> Matthew Weier O'Phinney
>> Project Lead| matt...@zend.com
>> Zend Framework  | http://framework.zend.com/
>

-- 
Matthew Weier O'Phinney
Project Lead| matt...@zend.com
Zend Framework  | http://framework.zend.com/


Re: [fw-general] Zend_Rest_Controller - n00b question

2009-09-07 Thread Edward Haber
Doesn't this component render Zend_Rest_Server moot? I'm sure I'm  
missing something, but Zend_Rest_Controller seems to provide a much  
better interface to managing REST contexts than Zend_Rest_Server as  
ZRC makes the route->to->context translation invisible. What did I miss?


Thanks!
Eddie


On Sep 7, 2009, at 11:11 AM, Matthew Weier O'Phinney wrote:


-- DorkFest  wrote
(on Monday, 07 September 2009, 07:28 AM -0700):

Is there any documentation on this component?

Can this controller be used to build an XML/JSON based API service?  
If so,
is there a built in method for retrieving an XML request fragment  
to the
controller (ie: not key/val pairs)? I think this is normally access  
with

something like: file_get_contents() from stdin. Wondering if ZF has a
built-in way.


You can pull in the raw request body from the request object:

  $body = $this->getRequest()->getRawBody();

With JSON data, it's trivial then to get your data:

  $data = Zend_Json::decode($body);

With XML, simply pass it to SimpleXML:

  $data = new SimpleXMLElement($body);

What I've done is to create an action helper that checks the "Accept"
request header ($request->getHeader('Accept')), and then sets the
"format" request parameter accordingly:

  // Accept headers often are of format: "application/json;  
charset=utf-8"

  $accept = $request->getHeader('Accept');
  $accept = explode(';', $accept);
  $accept = trim(array_shift($accept));

  switch ($accept) {
  case "application/json":
  $request->setParam('format', 'json');
  break;
  case "application/xml":
  $request->setParam('format', 'xml');
  break;
  }

Put the above code in the preDispatch() method of your action helper,
and then register that helper with the helper broker in your bootstrap
to ensure it's run for each controller requested.

Then, in your controller, you can initialize the ContextSwitch helper:

  $context = $this->_helper->contextSwitch;
  $context->setAutoJsonSerialization(false); // do this so you can  
have

 // JSON-specific views
  $context->addActionContext('index', 'xml')
  ->addActionContext('index', 'json')
  // ...

  $context->init();

If the context (xml or json, in the example above) is found (which  
will
be based on the "format" set previously), then it will attempt to  
render

a different view script: index.xml.phtml or index.json.phtml,
respectively. This allows you to keep your controller lean, and have
view-specific logic for each format. (For instance, I like to set my
response headers, such as Content-Type, Content-Range, etc., within my
views.)

I'm surprised there are very few posts and almost no documentation.  
Does

anyone have info on this component?


Well, the documentation is primarily centered around the basic usage  
of
the component, which is for creating RESTful resources in the MVC  
via a
combination of routing and specific actions in your controller. How  
you

handle different content types is something else entirely. I'll be
blogging on some of this information in the coming months, and also
covering it during my "Architecting Ajax Applications in Zend  
Framework"

tutorial at ZendCon.

--
Matthew Weier O'Phinney
Project Lead| matt...@zend.com
Zend Framework  | http://framework.zend.com/




Re: [fw-general] Zend_Rest_Controller - n00b question

2009-09-07 Thread Edward Haber

Ask the PHP gods and they shall provide...

A wealth of great information much appreciated. I look forward to the  
blog entries.


E

On Sep 7, 2009, at 11:11 AM, Matthew Weier O'Phinney wrote:


-- DorkFest  wrote
(on Monday, 07 September 2009, 07:28 AM -0700):

Is there any documentation on this component?

Can this controller be used to build an XML/JSON based API service?  
If so,
is there a built in method for retrieving an XML request fragment  
to the
controller (ie: not key/val pairs)? I think this is normally access  
with

something like: file_get_contents() from stdin. Wondering if ZF has a
built-in way.


You can pull in the raw request body from the request object:

   $body = $this->getRequest()->getRawBody();

With JSON data, it's trivial then to get your data:

   $data = Zend_Json::decode($body);

With XML, simply pass it to SimpleXML:

   $data = new SimpleXMLElement($body);

What I've done is to create an action helper that checks the "Accept"
request header ($request->getHeader('Accept')), and then sets the
"format" request parameter accordingly:

   // Accept headers often are of format: "application/json;  
charset=utf-8"

   $accept = $request->getHeader('Accept');
   $accept = explode(';', $accept);
   $accept = trim(array_shift($accept));

   switch ($accept) {
   case "application/json":
   $request->setParam('format', 'json');
   break;
   case "application/xml":
   $request->setParam('format', 'xml');
   break;
   }

Put the above code in the preDispatch() method of your action helper,
and then register that helper with the helper broker in your bootstrap
to ensure it's run for each controller requested.

Then, in your controller, you can initialize the ContextSwitch helper:

   $context = $this->_helper->contextSwitch;
   $context->setAutoJsonSerialization(false); // do this so you can  
have

  // JSON-specific views
   $context->addActionContext('index', 'xml')
   ->addActionContext('index', 'json')
   // ...

   $context->init();

If the context (xml or json, in the example above) is found (which  
will
be based on the "format" set previously), then it will attempt to  
render

a different view script: index.xml.phtml or index.json.phtml,
respectively. This allows you to keep your controller lean, and have
view-specific logic for each format. (For instance, I like to set my
response headers, such as Content-Type, Content-Range, etc., within my
views.)

I'm surprised there are very few posts and almost no documentation.  
Does

anyone have info on this component?


Well, the documentation is primarily centered around the basic usage  
of
the component, which is for creating RESTful resources in the MVC  
via a
combination of routing and specific actions in your controller. How  
you

handle different content types is something else entirely. I'll be
blogging on some of this information in the coming months, and also
covering it during my "Architecting Ajax Applications in Zend  
Framework"

tutorial at ZendCon.

--
Matthew Weier O'Phinney
Project Lead| matt...@zend.com
Zend Framework  | http://framework.zend.com/




Re: [fw-general] Zend_Rest_Controller - n00b question

2009-09-07 Thread Matthew Weier O'Phinney
-- DorkFest  wrote
(on Monday, 07 September 2009, 07:28 AM -0700):
> Is there any documentation on this component? 
> 
> Can this controller be used to build an XML/JSON based API service? If so,
> is there a built in method for retrieving an XML request fragment to the
> controller (ie: not key/val pairs)? I think this is normally access with
> something like: file_get_contents() from stdin. Wondering if ZF has a
> built-in way.

You can pull in the raw request body from the request object:

$body = $this->getRequest()->getRawBody();

With JSON data, it's trivial then to get your data:

$data = Zend_Json::decode($body);

With XML, simply pass it to SimpleXML:

$data = new SimpleXMLElement($body);

What I've done is to create an action helper that checks the "Accept"
request header ($request->getHeader('Accept')), and then sets the
"format" request parameter accordingly:

// Accept headers often are of format: "application/json; charset=utf-8"
$accept = $request->getHeader('Accept');
$accept = explode(';', $accept);
$accept = trim(array_shift($accept));

switch ($accept) {
case "application/json":
$request->setParam('format', 'json');
break;
case "application/xml":
$request->setParam('format', 'xml');
break;
}

Put the above code in the preDispatch() method of your action helper,
and then register that helper with the helper broker in your bootstrap
to ensure it's run for each controller requested.

Then, in your controller, you can initialize the ContextSwitch helper:

$context = $this->_helper->contextSwitch;
$context->setAutoJsonSerialization(false); // do this so you can have
   // JSON-specific views
$context->addActionContext('index', 'xml')
->addActionContext('index', 'json')
// ...

$context->init();

If the context (xml or json, in the example above) is found (which will
be based on the "format" set previously), then it will attempt to render
a different view script: index.xml.phtml or index.json.phtml,
respectively. This allows you to keep your controller lean, and have
view-specific logic for each format. (For instance, I like to set my
response headers, such as Content-Type, Content-Range, etc., within my
views.)

> I'm surprised there are very few posts and almost no documentation. Does
> anyone have info on this component?

Well, the documentation is primarily centered around the basic usage of
the component, which is for creating RESTful resources in the MVC via a
combination of routing and specific actions in your controller. How you
handle different content types is something else entirely. I'll be
blogging on some of this information in the coming months, and also
covering it during my "Architecting Ajax Applications in Zend Framework"
tutorial at ZendCon.

-- 
Matthew Weier O'Phinney
Project Lead| matt...@zend.com
Zend Framework  | http://framework.zend.com/


[fw-general] Zend_Rest_Controller - n00b question

2009-09-07 Thread DorkFest

Is there any documentation on this component? 

Can this controller be used to build an XML/JSON based API service? If so,
is there a built in method for retrieving an XML request fragment to the
controller (ie: not key/val pairs)? I think this is normally access with
something like: file_get_contents() from stdin. Wondering if ZF has a
built-in way.

I'm surprised there are very few posts and almost no documentation. Does
anyone have info on this component?

Thanks,
Eddie



-- 
View this message in context: 
http://www.nabble.com/Zend_Rest_Controller---n00b-question-tp25331418p25331418.html
Sent from the Zend Framework mailing list archive at Nabble.com.