<?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 - Tag - mvc</title>
  <link>http://mirmodynamics.com/</link>
  <atom:link href="http://mirmodynamics.com/feed/tag/mvc/rss2" rel="self" type="application/rss+xml"/>
  <description>Si tu kiffes pas reunoi, t'écoutes pas et puis c'est tout.</description>
  <language>en</language>
  <pubDate>Sun, 14 Mar 2010 19:59:01 +0100</pubDate>
  <copyright>2003-2009 &amp;copy; Geoffrey Bachelet</copyright>
  <docs>http://blogs.law.harvard.edu/tech/rss</docs>
  <generator>Dotclear</generator>
  
    
  <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/atom/comments/887</wfw:commentRss>
      </item>
    
</channel>
</rss>