{"id":5353,"date":"2014-01-30T09:24:35","date_gmt":"2014-01-30T00:24:35","guid":{"rendered":"http:\/\/www.moonmile.net\/blog\/?p=5353"},"modified":"2014-01-28T17:18:38","modified_gmt":"2014-01-28T08:18:38","slug":"cakephp%e3%81%aeweb-api%e3%83%ac%e3%82%b9%e3%83%9d%e3%83%b3%e3%82%b9%e3%82%92xml%e5%9e%8b%e5%bc%8f%e3%81%a7%e8%bf%94%e3%81%99","status":"publish","type":"post","link":"http:\/\/www.moonmile.net\/blog\/archives\/5353","title":{"rendered":"CakePHP\u306eWeb API\u30ec\u30b9\u30dd\u30f3\u30b9\u3092XML\u578b\u5f0f\u3067\u8fd4\u3059"},"content":{"rendered":"<p>CakePHP \u306e\u901a\u5e38 View \u304c\u3067\u304d\u305f\u306e\u3067\u3001XML \u3092\u8fd4\u3059 Web API\u7528\u306e\u30d3\u30e5\u30fc\u3092\u4f5c\u308a\u307e\u3059\u3002<\/p>\n<p>[CakePHP] REST\u3092\u4f7f\u3063\u3066View\u3092XML\u5f62\u5f0f\u3067\u8fd4\u3059 | Moonmile Solutions Blog<br \/>\n<a href=\"http:\/\/www.moonmile.net\/blog\/archives\/4871\">http:\/\/www.moonmile.net\/blog\/archives\/4871<\/a><\/p>\n<p>\u3068\u57fa\u672c\u306f\u540c\u3058\u3067\u3059\u3002View \u306e\u5404\u30d5\u30a9\u30eb\u30c0\u306b xml \u30d5\u30a9\u30eb\u30c0\u3092\u4f5c\u6210\u3057\u3066\u5bfe\u5fdc\u3055\u305b\u307e\u3059\u3002\u3044\u3061\u3044\u3061\u540c\u3058\u30d5\u30a1\u30a4\u30eb\u3092\u4f5c\u3089\u306a\u3044\u3068\u3044\u3051\u306a\u3044\u306e\u304c\u9762\u5012\u3067\u3059\u304c\u3001\u9069\u5f53\u306a\u30b9\u30af\u30ea\u30d7\u30c8\u3092\u4f5c\u3063\u3066\u81ea\u52d5\u5316\u3055\u305b\u3066\u304a\u304f\u3068\u3088\u3044\u3067\u3057\u3087\u3046\u3002<\/p>\n<p>1.app\/Config\/routes.php \u3092\u66f8\u304d\u63db\u3048\u308b<\/p>\n<p>\u5148\u982d\u306e\u884c\u306b\u4ee5\u4e0b\u3092\u8ffd\u52a0\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n\tRouter::parseExtensions('xml');\r\n<\/pre>\n<p>2.app\/Controller\/AppController.php \u3092\u66f8\u304d\u63db\u3048\u308b<\/p>\n<p>RequestHandler \u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u3092\u6709\u52b9\u306b\u3059\u308b\u3053\u3068\u3001\u30da\u30fc\u30b8\u30f3\u30b0\u306e\u6570\u3092\u591a\u3081\u306b\u3068\u3063\u3066\u304a\u304d\u307e\u3059\u3002<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\nclass AppController extends Controller {\r\n\tpublic $components = array(&#039;RequestHandler&#039;);\r\n\tpublic $paginate = array( &#039;limit&#039; =&gt; 1000 );\r\n}\r\n<\/pre>\n<p>\u30c7\u30d5\u30a9\u30eb\u30c8\u3067\u306f 20 \u4ef6\u3050\u3089\u3044\u306a\u306e\u3067\u3059\u304c\u3001XML\u578b\u5f0f\u306e\u5834\u5408\u306f\u305d\u308c\u3067\u306f\u4e0d\u4fbf\u3067\u3059\u3002\u306a\u306e\u30671000\u4ef6\u3092limit\u306b\u3057\u3066\u304a\u304d\u307e\u3059\u3002\u672c\u5f53\u306f\u30b3\u30f3\u30c8\u30ed\u30fc\u30e9\u3054\u3068\u306b\u7d30\u304b\u304f\u5236\u9650\u3059\u308b\u306e\u304c\u3088\u3044\u306e\u3067\u3059\u304c\u3001\u305d\u308c\u306f\u30b7\u30b9\u30c6\u30e0\u7279\u6027\u3068\u3044\u3046\u3068\u3067\u3001\u3072\u3068\u307e\u305a\u3053\u3046\u3057\u3066\u304a\u304f\u3068\u5927\u4e08\u592b\u3067\u3059\u3002\u3053\u306e\u6570\u3092\u3082\u3063\u3068\u5927\u304d\u304f\u3057\u3066\u3082\u3044\u3044\u306e\u3067\u3059\u304c\u3001\u30ec\u30b9\u30dd\u30f3\u30b9\u3082\u60aa\u304f\u306a\u308b\u3057\u3001\u30b7\u30b9\u30c6\u30e0\u7684\u306b\u3053\u306e\u3050\u3089\u3044\u304c\u3057\u3066\u304a\u304f\u3001\u3068\u3044\u3046\u611f\u3058\u3067\u3059\u306d\u3002<\/p>\n<p>3.XML \u7528\u306e View \u3092\u4f5c\u6210\u3059\u308b<\/p>\n<p>Controller \u3067 set \u3057\u305f\u5024\u3092 xml\/*.ctp \u30d5\u30a1\u30a4\u30eb\u306b\u66f8\u3044\u3066\u3044\u304d\u307e\u3059\u3002CURD \u3059\u3079\u3066\u306b\u5bfe\u5fdc\u3059\u308b\u305f\u3081\u306b\u3001index.ctp, view.ctp, add.ctp, edit.ctp \u3092\u4f5c\u3063\u3066\u304a\u3051\u3070 OK \u3067\u3059\u3002<\/p>\n<p>index.ctp \u306f\u3001\u914d\u5217\u306b\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n$items = array(&#039;items&#039;=&gt; array(&#039;item&#039;=&gt; $stores));\r\n$xml = Xml::fromArray(array(&#039;response&#039;=&gt;$items));\r\necho $xml-&gt;asXML();\r\n<\/pre>\n<p>view.ctp, add.ctp, edit.ctp \u306f\u4e00\u3064\u3060\u3051\u306e\u8981\u7d20\u3092\u8fd4\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n$item = array(&#039;item&#039;=&gt; $store);\r\n$xml = Xml::fromArray(array(&#039;response&#039;=&gt;$item));\r\necho $xml-&gt;asXML();\r\n<\/pre>\n<p>response \u8981\u7d20\u3092\u4f5c\u3063\u3066\u3001\u305d\u306e\u4e2d\u306b items \u3042\u308b\u3044\u306f item \u3092\u4f5c\u3063\u3066\u3044\u307e\u3059\u304c\u3001\u3053\u308c\u306f\u597d\u307f\u3067\u3059\u306d\u3002\u3069\u3061\u3089\u3082\u30eb\u30fc\u30c8\u8981\u7d20\u306f\u3072\u3068\u3064\u306b\u306a\u308b\u306e\u3067\u3001response \u306f\u5fc5\u8981\u306a\u3044\u306e\u3067\u3059\u304c\u3001\u53d7\u3051\u53d6\u308b\u5074\uff08\u3053\u306e\u5834\u5408\u306f WPF \uff09\u3067\u3001\u540c\u3058\u540d\u524d\u306e\u307b\u3046\u304c\u3044\u3044\u3060\u308d\u3046\u3068\u601d\u3063\u3066\u3053\u3046\u3057\u3066\u3044\u307e\u3059\u3002<br \/>\n\u305f\u3060\u3057\u3001\u3053\u308c\u3060\u3068 web api \u306e OK\/NG \u304c\u8fd4\u3057\u306b\u304f\u3044\u306e\u304c\u96e3\u70b9\u3067\u3059\u3002Web API \u306e\u53d7\u3051\u53d6\u308a\u5074\u306e\u3064\u304f\u308a\u3068\u3057\u3066\u306f\u3001<\/p>\n<ul>\n<li>API\u81ea\u4f53\u306e\u6210\u529f\/\u5931\u6557\u3092\u65e9\u3081\u306b\u62fe\u3044\u305f\u3044<\/li>\n<li>API\u30ec\u30b9\u30dd\u30f3\u30b9\u306e\u578b\u5f0f\u306f\u3067\u304d\u308b\u3060\u3051\u540c\u3058\u5f62\u5f0f\u3067\u3042\u3063\u3066\u307b\u3057\u3044\u3002<\/li>\n<li>OK\/NG\u306e\u30ec\u30b9\u30dd\u30f3\u30b9\u306f\u3067\u304d\u308b\u3060\u3051\u540c\u3058\u5f62\u5f0f\u3067\u3042\u3063\u3066\u307b\u3057\u3044\u3002<\/li>\n<\/ul>\n<p>\u3068\u3044\u3046\u306e\u304c\u3042\u308a\u307e\u3059\u3002XML\u3092\u30d1\u30fc\u30b9\u3059\u308b\u3068\u304d\u306b\u3001\u6307\u5b9a\u306e\u8981\u7d20\u304c\u3042\u3063\u305f\u308a\u306a\u304b\u3063\u305f\u308a\u3059\u306e\u306f\u7d50\u69cb\u9762\u5012\u3067\u3059\u3002\u307e\u305f\u3001\u6b63\u5e38\u6642\u306eXML\u69cb\u9020\u3068\u7570\u5e38\u6642\u306bXML\u69cb\u9020\u306f\u540c\u3058\u3067\u3042\u3063\u305f\u307b\u3046\u304c\u30d7\u30ed\u30b0\u30e9\u30e0\u304c\u7c21\u5358\u306b\u306a\u308a\u307e\u3059\u3002\u305d\u3046\u3044\u610f\u5473\u3067\u306f\u3001\u3042\u3089\u304b\u3058\u3081 response \u306e\u5c5e\u6027\u3068\u3057\u3066 OK\/NG \u3092\u3064\u3051\u305f\u308a\u30d0\u30fc\u30b8\u30e7\u30f3\u3092\u3064\u3051\u308b\u306e\u304c\u3088\u3044\u306e\u3067\u3059\u304c&#8230;\u4eca\u56de\u306f\u3001\u6700\u521d\u306e\u5b9f\u88c5\u6642\u306b\u5165\u308c\u5fd8\u308c\u305f\u306e\u3053\u306e\u307e\u307e\u3067\u3059\u3002<br \/>\n\u3042\u3068\u3001xml\/*.ctp \u81ea\u4f53\u306f\u3001\u975e\u5e38\u306b\u5358\u7d14\u306b\u3057\u3066\u304a\u304b\u306a\u3044\u3068\u30c7\u30d0\u30c3\u30b0\u304c\u9762\u5012\u306a\u3093\u3067\u3059\u3088\u306d\u3002\u306a\u306e\u3067\u3001\u3067\u304d\u308b\u3060\u3051\u7c21\u5358\u306b\u3059\u308b\u305f\u3081\u306b\u3001\u3053\u3093\u306a\u30b9\u30bf\u30a4\u30eb\u306b\u3057\u3066\u307e\u3059\u3002<\/p>\n<p>\u25a0\u4e00\u89a7(index)\u3092\u5b9f\u884c\u3059\u308b\u3068<\/p>\n<p>XML \u578b\u5f0f\u3067\u8fd4\u3059\u5834\u5408\u306f\u3001http:\/\/localhost:81\/cakephp-2.4.5\/Stores.xml \u306e\u3088\u3046\u306b\u3001\u6700\u5f8c\u306b\u300c.xml\u300d\u3092\u3064\u3051\u307e\u3059\u3002\u3053\u3046\u3059\u308b\u3068\u3001$belongsTo \u306a\u3069\u3067\u6307\u5b9a\u3057\u305f\u30a2\u30bd\u30b7\u30a8\u30fc\u30b7\u30e7\u30f3\u3068\u4e00\u7dd2\u306b\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3067\u304d\u307e\u3059\u3002<\/p>\n<p><a href=\"http:\/\/www.moonmile.net\/blog\/wp-content\/uploads\/2014\/01\/wpid-dworkblogimage20140128_05org.jpg\"><img decoding=\"async\" src=\"http:\/\/www.moonmile.net\/blog\/wp-content\/uploads\/2014\/01\/wpid-dworkblogimage20140128_05thum.jpg\" border=\"0\" \/><\/a><\/p>\n<p>\u30ec\u30b9\u30dd\u30f3\u30b9\u81ea\u4f53\u306b\u306f\u4e00\u5207\u6539\u884c\u304c\u306a\u3044\u306e\u3067\u95b2\u89a7\u306b\u306f\u4e0d\u4fbf\u306a\u306e\u3067\u3001IE\u306a\u3069\u306e\u30d6\u30e9\u30a6\u30b6\u3092\u5229\u7528\u3059\u308b\u3068\u3088\u3044\u3067\u3059\u3002<\/p>\n<p>\u25a0\u3072\u3068\u3064\u306e\u8981\u7d20\uff08view\uff09\u3092\u5b9f\u884c\u3059\u308b<\/p>\n<p>ID \u3092\u6307\u5b9a\u3057\u3066\u8868\u793a\u3057\u305f\u3044\u5834\u5408\u306f\u3001http:\/\/localhost:81\/cakephp-2.4.5\/Stores\/view\/20.xml \u306e\u3088\u3046\u306b\u6307\u5b9a\u3057\u307e\u3059\u3002\u6700\u5f8c\u306b\u300c.xml\u300d\u3092\u3064\u3051\u308c\u3070\u3044\u3044\u306e\u3067\u3001\u4ed6\u306e edit \u3084 add \u3082\u540c\u3058\u3067\u3059\u3002<\/p>\n<p><a href=\"http:\/\/www.moonmile.net\/blog\/wp-content\/uploads\/2014\/01\/wpid-dworkblogimage20140128_06org.jpg\"><img decoding=\"async\" src=\"http:\/\/www.moonmile.net\/blog\/wp-content\/uploads\/2014\/01\/wpid-dworkblogimage20140128_06thum.jpg\" border=\"0\" \/><\/a><\/p>\n<p>\u4f3c\u305f\u611f\u3058\u306b\u898b\u3048\u307e\u3059\u304c\u3001\u4e00\u89a7\u306e\u5834\u5408\u306f response\/items\/item \u306e\u4e2d\u306b\u305d\u308c\u305e\u308c\u306e\u8981\u7d20\u304c\u5165\u3063\u3066\u3044\u3066\u3001view \u306e\u5834\u5408\u306f response\/item \u306e\u4e2d\u306b\u8981\u7d20\u304c\u5165\u3063\u3066\u3044\u307e\u3059\u3002<br \/>\n\u3053\u306e\u5165\u308c\u5b50\u304c\u9762\u5012\u306a\u5834\u5408\u306f\u3001<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n$items = array(&#039;items&#039;=&gt; array(&#039;item&#039;=&gt; $store));\r\n$xml = Xml::fromArray(array(&#039;response&#039;=&gt;$items));\r\necho $xml-&gt;asXML();\r\n<\/pre>\n<p>\u306e\u3088\u3046\u306b items \u8981\u7d20\u3092\u5165\u308c\u3066\u3057\u307e\u3063\u3066\u3082\u3044\u3044\u3068\u601d\u3044\u307e\u3059\u3002\u3053\u3046\u3059\u308b\u3053\u3068\u3067\u3001\u3072\u3068\u3064\u3067\u3082\u8907\u6570\u306e\u8981\u7d20\u3067\u3082\u3001response\/items\/item \u306e\u968e\u5c64\u3067\u30c7\u30fc\u30bf\u304c\u53d6\u308c\u307e\u3059\u3002\u3053\u3063\u3061\u306e\u307b\u3046\u304c\u3044\u3044\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u306d\u3002<\/p>\n<p>\u25a0add\/edit \u306f\u3069\u3046\u3059\u308b\u306e\u304b\uff1f<\/p>\n<p>add \u3082 edit \u3082\u30ec\u30b9\u30dd\u30f3\u30b9\u3068\u3057\u3066\u306f view \u3068\u540c\u3058\u3067\u3059\u3002\u304c\u3001\u65b0\u898f\u306b\u4f5c\u6210\u3057\u305f\u308a\u7de8\u96c6\u3057\u305f\u308a\u3059\u308b\u30c7\u30fc\u30bf\u3092\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u304b\u3089\u9001\u3089\u306a\u3044\u3068\u3044\u3051\u307e\u305b\u3093\u3002\u30d6\u30e9\u30a6\u30b6\u304b\u3089\u7de8\u96c6\u3059\u308b\u30da\u30fc\u30b8\u306f cake bake \u3067\u81ea\u52d5\u751f\u6210\u3057\u3066\u3044\u308b\u306e\u3067\u3001WPF \u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u304b\u3089\u30b5\u30fc\u30d0\u30fc\u3078 POST \u3059\u308b\u30d7\u30ed\u30b0\u30e9\u30e0\u3092\u4f5c\u308a\u307e\u3059\u3002\u3053\u308c\u306f\u3061\u3087\u3063\u3068\u6b21\u56de\u3078\u3002<\/p>\n<p>\u25a0\u304a\u307e\u3051<\/p>\n<p>\u9069\u5f53\u3067\u3059\u304c\u3001\u3053\u3093\u306a\u98a8\u306b\u30b9\u30af\u30ea\u30d7\u30c8\u3092\u66f8\u3044\u3066\u3001xml\/*.ctp \u3092\u4f5c\u6210\u3057\u307e\u3059\u3002<br \/>\napp \u30d5\u30a9\u30eb\u30c0\u306b allbakexml.php \u306e\u30d5\u30a1\u30a4\u30eb\u3067\u4fdd\u5b58\u3057\u3066\u5b9f\u884c\u3057\u3066\u304f\u3060\u3055\u3044\u3002$views \u306b\u8907\u6570\u5f62\u3001\u5358\u6570\u5f62\u306e\u9806\u306b\u66f8\u304b\u306a\u3044\u3068\u30c0\u30e1\u306a\u306e\u306f\u3001Controller \u304b Model \u306e\u4e2d\u8eab\u3092\u898b\u308b\u3088\u3046\u306b\u3059\u308c\u3070\u6c4e\u7528\u5316\u3067\u304d\u308b\u304b\u3082\u3002<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n&lt;?php\r\n$views = array( \r\n\tarray(&#039;adjustmentjobs&#039;, &#039;adjustmentjob&#039;),\r\n...\r\n\tarray(&#039;stores&#039;, &#039;store&#039;),\r\n\tarray(&#039;workingsituations&#039;, &#039;workingsituation&#039;),\r\n);\r\n\r\nforeach ( $views as $v ) \r\n{\r\n\t$v0 = $v&#x5B;0];\r\n\t$v1 = $v&#x5B;1];\r\n\t\r\n\t$xml0 =&lt;&lt;&lt; END\r\n&lt;?php \r\n$items = array(&#039;items&#039;=&gt; array(&#039;item&#039;=&gt; $$v0));\r\n$xml = Xml::fromArray(array(&#039;response&#039;=&gt;$items));\r\necho $xml-&gt;asXML();\r\n\r\nEND;\r\n\r\n\tmkdir(&amp;quot;view\/$v0\/xml&amp;quot;);\r\n\t$path=&amp;quot;view\/$v0\/xml\/index.ctp&amp;quot;;\r\n#\tprint &amp;quot;$xml0n&amp;quot;;\r\n\tfile_put_contents( $path, $xml0 );\r\n\r\n\t$xml1 =&lt;&lt;&lt; END\r\n&lt;?php \r\n$item = array(&#039;item&#039;=&gt; $$v1);\r\n$xml = Xml::fromArray(array(&#039;response&#039;=&gt;$item));\r\necho $xml-&gt;asXML();\r\n\r\nEND;\r\n\r\n#\tprint &amp;quot;$xml0n&amp;quot;;\r\n\tfile_put_contents( &amp;quot;view\/$v0\/xml\/view.ctp&amp;quot;, $xml1 );\r\n\tfile_put_contents( &amp;quot;view\/$v0\/xml\/add.ctp&amp;quot;, $xml1 );\r\n\tfile_put_contents( &amp;quot;view\/$v0\/xml\/edit.ctp&amp;quot;, $xml1 );\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>CakePHP \u306e\u901a\u5e38 View \u304c\u3067\u304d\u305f\u306e\u3067\u3001XML \u3092\u8fd4\u3059 Web API\u7528\u306e\u30d3\u30e5\u30fc\u3092\u4f5c\u308a\u307e\u3059\u3002 [CakePHP] REST\u3092\u4f7f\u3063\u3066View\u3092XML\u5f62\u5f0f\u3067\u8fd4\u3059 | Moonmile Solutions Blog h &hellip; <a href=\"http:\/\/www.moonmile.net\/blog\/archives\/5353\">\u7d9a\u304d\u3092\u8aad\u3080 <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[20,60],"tags":[],"class_list":["post-5353","post","type-post","status-publish","format-standard","hentry","category-cakephp","category-wpf"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/posts\/5353","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/comments?post=5353"}],"version-history":[{"count":3,"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/posts\/5353\/revisions"}],"predecessor-version":[{"id":5356,"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/posts\/5353\/revisions\/5356"}],"wp:attachment":[{"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/media?parent=5353"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/categories?post=5353"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/tags?post=5353"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}