<?xml version="1.0" encoding="utf-8"?><?xml-stylesheet title="XSL formatting" type="text/xsl" href="http://mirmodynamics.com/feed/rss2/xslt" ?><rss version="2.0"
  xmlns:dc="http://purl.org/dc/elements/1.1/"
  xmlns:wfw="http://wellformedweb.org/CommentAPI/"
  xmlns:content="http://purl.org/rss/1.0/modules/content/"
  xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
  <title>Mirmo Dynamics - controller</title>
  <link>http://mirmodynamics.com/</link>
  <atom:link href="http://mirmodynamics.com/feed/tag/controller/rss2" rel="self" type="application/rss+xml"/>
  <description>Rien de grand ne se fit jamais sans enthousiasme.</description>
  <language>en</language>
  <pubDate>Wed, 08 Oct 2008 11:22:01 +0200</pubDate>
  <copyright>2003-2008 &amp;copy; Geoffrey Bachelet</copyright>
  <docs>http://blogs.law.harvard.edu/tech/rss</docs>
  <generator>Dotclear</generator>
  
    
  <item>
    <title>Accessing raw post data in a controller</title>
    <link>http://mirmodynamics.com/post/2007/11/21/Accessing-raw-post-data-in-a-controller</link>
    <guid isPermaLink="false">urn:md5:d8a3050d140c3d8fac1e1de762628c9d</guid>
    <pubDate>Wed, 21 Nov 2007 12:03:00 +0100</pubDate>
    <dc:creator>Geoffrey</dc:creator>
        <category>Coding</category>
        <category>controller</category><category>http</category><category>post</category><category>raw post data</category><category>zend framework</category>    
    <description>    &lt;p&gt;For some reason, &lt;code&gt;$HTTP_RAW_POST_DATA&lt;/code&gt; does not seem to be set inside an action controller. You'll have to use the &lt;code&gt;php://input&lt;/code&gt; stream wrapper to access raw http post data:&lt;/p&gt;

&lt;code class=&quot;php&quot;&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;$raw_post_data&lt;/span&gt; = &lt;a href=&quot;http://www.php.net/file_get_contents&quot;&gt;&lt;span style=&quot;color: #000066;&quot;&gt;file_get_contents&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'php://input'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;/code&gt;</description>
    
    
    
          <comments>http://mirmodynamics.com/post/2007/11/21/Accessing-raw-post-data-in-a-controller#comment-form</comments>
      <wfw:comment>http://mirmodynamics.com/post/2007/11/21/Accessing-raw-post-data-in-a-controller#comment-form</wfw:comment>
      <wfw:commentRss>http://mirmodynamics.com/feed/rss2/comments/1075</wfw:commentRss>
      </item>
    
  <item>
    <title>A Zend controller plugin to enable RESTful behaviour</title>
    <link>http://mirmodynamics.com/post/2007/07/14/A-Zend-controller-plugin-to-enable-REST-like-behaviour</link>
    <guid isPermaLink="false">urn:md5:a7322d8fa8be9d89dc76a2193831cfe0</guid>
    <pubDate>Sat, 14 Jul 2007 13:33:00 +0200</pubDate>
    <dc:creator>Geoffrey</dc:creator>
        <category>Coding</category>
        <category>controller</category><category>mvc</category><category>php</category><category>plugin</category><category>REST</category><category>zend framework</category>    
    <description>&lt;p&gt;This is a simple controller plugin for the &lt;a href=&quot;http://framework.zend.com/&quot;&gt;Zend Framework&lt;/a&gt; which enable &lt;acronym&gt;REST&lt;/acronym&gt;ful behaviour. It basically adds the &lt;acronym&gt;HTTP&lt;/acronym&gt; method name to the action name, so that the &lt;acronym&gt;URL&lt;/acronym&gt; &lt;code&gt;http://example.com/foo/bar&lt;/code&gt; will be dispatched to &lt;code&gt;FooController::barGetAction&lt;/code&gt; on a GET, &lt;code&gt;FooController::barPostAction&lt;/code&gt; on a POST, etc.&lt;/p&gt;    &lt;p&gt;Here is the actual code:&lt;/p&gt;

&lt;pre&gt;
&amp;lt;?php

class My_Controller_Plugin_Rest extends Zend_Controller_Plugin_Abstract {

        /**
         * Defines the format of the REST action name
         * Quite useless atm as the dispatcher will strip
         * any non alpha character
         */

        protected $_nameFormat = ':action:method';

        /**
         * Rewrites the action according to the http method
         */

        public function preDispatch() {
                $request = $this-&amp;gt;getRequest();
                $restActionName = $this-&amp;gt;_translateSpec($this-&amp;gt;_nameFormat, array(
                        'action' =&amp;gt; $request-&amp;gt;getActionName(),
                        'method' =&amp;gt; $request-&amp;gt;getMethod(),
                ));
                $request-&amp;gt;setActionName($restActionName);
        }

        /**
         * Inject values into a spec strings
         *
         * Allowed values are:
         *      :action =&amp;gt; the action name
         *      :method =&amp;gt; the http method
         *
         * @param string $spec
         * @param array $vars
         * @return string
         */

        protected function _translateSpec($spec, $vars = array()) {
                foreach($vars as $key =&amp;gt; $value) {
                        switch($key) {
                                case 'action':
                                case 'method':
                                        $$key = $value;
                                break;
                                default:
                                break;
                        }
                }

                $replacements = array(
                        ':action' =&amp;gt; $action,
                        ':method' =&amp;gt; $method,
                );

                $value = str_replace(array_keys($replacements), array_values($replacements),$spec);
                return $value;
        }
}
&lt;/pre&gt;


&lt;p&gt;Still, i'm not completly satisfied with this plugin. Plugins certainly allows for powerful control over what's going up in the dispatch process, but the dispatcher itself enforces a set of rules on actions naming (eg, you can't have a _ in it, it is stripped at dispatch time). Thus, I'm wondering on the pertinence of writting a custom dispatcher (read &lt;code&gt;My_Controller_Dispatcher_Rest&lt;/code&gt;) instead of just a plugin, which would enable far more possibilities.&lt;/p&gt;


&lt;p&gt;Btw, in case you're wondering, the plugins is used like this;&lt;/p&gt;

&lt;pre&gt;
$frontController = Zend_Controller_Front::getInstance();
$frontController-&amp;gt;registerPlugin(new My_Controller_Plugin_Rest);
&lt;/pre&gt;


&lt;p&gt;Easy heh ?&lt;/p&gt;


&lt;p&gt;Also, I'm not convinced that this plugin is &lt;em&gt;the way to go&lt;/em&gt; in matter of RESTful functionnality. I'm still wondering if it would not be better to have urls mapped to a single controller, replacing actions with &lt;em&gt;http methods&lt;/em&gt; (that is, &lt;code&gt;http://example.com/foo/bar&lt;/code&gt; would map to &lt;code&gt;FooController::getAction&lt;/code&gt;, etc).&lt;/p&gt;


&lt;p&gt;Any opinions around ?&lt;/p&gt;</description>
    
    
    
          <comments>http://mirmodynamics.com/post/2007/07/14/A-Zend-controller-plugin-to-enable-REST-like-behaviour#comment-form</comments>
      <wfw:comment>http://mirmodynamics.com/post/2007/07/14/A-Zend-controller-plugin-to-enable-REST-like-behaviour#comment-form</wfw:comment>
      <wfw:commentRss>http://mirmodynamics.com/feed/rss2/comments/887</wfw:commentRss>
      </item>
    
</channel>
</rss>