{"id":664,"date":"2015-08-16T18:55:50","date_gmt":"2015-08-16T09:55:50","guid":{"rendered":"http:\/\/gomocool.net\/gomokulog\/?p=664"},"modified":"2015-08-16T18:55:50","modified_gmt":"2015-08-16T09:55:50","slug":"cakephp3-%e3%81%96%e3%81%a3%e3%81%8f%e3%82%8a%e3%81%a8%e3%83%95%e3%82%a9%e3%83%bc%e3%83%a0%e8%aa%8d%e8%a8%bc","status":"publish","type":"post","link":"https:\/\/gomokulog.gomocool.net\/?p=664","title":{"rendered":"[CakePHP3] \u3056\u3063\u304f\u308a\u3068\u30d5\u30a9\u30fc\u30e0\u8a8d\u8a3c"},"content":{"rendered":"<p>\u30d5\u30a9\u30fc\u30e0(Form)\u306b\u3088\u308b\u8a8d\u8a3c\u6a5f\u80fd\u3092\u8ffd\u52a0\u3059\u308b\u3002<\/p>\n<p>\u30e6\u30fc\u30b6\u30fc\u30c6\u30fc\u30d6\u30eb\u306f\u3001<a href=\"http:\/\/gomocool.net\/gomokulog\/?p=640\">\u524d\u306e\u8a18\u4e8b<\/a>\u3092\u53c2\u8003\u306b\u3002<\/p>\n<p>AppController.php\u306einitialize\u30e1\u30bd\u30c3\u30c9\u306b\u8a8d\u8a3c\u95a2\u9023\u306e\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u3092\u8a2d\u5b9a\u3002<\/p>\n<pre class=\"lang:php decode:true\">public function initialize()\r\n{\r\n    parent::initialize();\r\n    $this-&gt;loadComponent('Flash');\r\n    \/\/\u8a8d\u8a3c\r\n    $this-&gt;loadComponent('Auth',[\r\n\t'authenticate' =&gt; [\r\n\t\t'Form' =&gt; [\r\n\t\t\t'fields' =&gt; [\r\n\t\t\t\t'username' =&gt; 'email',\r\n\t\t\t\t'password' =&gt; 'password'\r\n\t\t\t]\r\n\t\t]\r\n\t],\r\n\t'loginAction' =&gt; [\r\n\t\t'controller' =&gt; 'Users',\r\n\t\t'action' =&gt; 'login'\r\n\t]\r\n]);\r\n}<\/pre>\n<p>Auth\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u3092\u30ed\u30fc\u30c9\u3057\u3066\u3001\u8a8d\u8a3c\u65b9\u6cd5(authenticate)\u3068\u3057\u3066\u3001Form\u3092\u4f7f\u3063\u3066\u3001username\u306bemal\u3092\u3001password\u306bpassword\u306e\u30ab\u30e9\u30e0\u540d\u3092\u4f7f\u3046\u3002<br \/>\n\u30ed\u30b0\u30a4\u30f3\u306f\u3001Users\u30b3\u30f3\u30c8\u30ed\u30fc\u30e9\u30fc\u306elogin\u30a2\u30af\u30b7\u30e7\u30f3\u3067\u884c\u3046\u3002<\/p>\n<p>\u3053\u308c\u3067\u3001\u3069\u306e\u30da\u30fc\u30b8\u306b\u884c\u3063\u3066\u3082\u3001AppController\u30af\u30e9\u30b9\u3092\u7d99\u627f\u3057\u3066\u3044\u308b\u30da\u30fc\u30b8\u3067\u306f\u3001Users-&gt;login\u306b\u98db\u3070\u3055\u308c\u308b\u3088\u3046\u306b\u306a\u308b\u3002<\/p>\n<p>\u4eca\u306e\u3068\u3053\u308d\u809d\u5fc3\u306eUsers-&gt;login\u304c\u7121\u3044\u306e\u3067\u3001\u8ffd\u52a0\u3059\u308b\u3002<\/p>\n<p>Users\u30b3\u30f3\u30c8\u30ed\u30fc\u30e9\u30fc\u306b\u3001login\u30a2\u30af\u30b7\u30e7\u30f3\u3092\u8ffd\u52a0\u3002<\/p>\n<pre class=\"lang:php decode:true\">\/**\r\n * \u30ed\u30b0\u30a4\u30f3\u30da\u30fc\u30b8\r\n * @return type\r\n *\/\r\npublic function login()\r\n{\r\n\tif($this-&gt;request-&gt;is('post')){\r\n\t\t$user = $this-&gt;Auth-&gt;identify();\r\n\t\tif($user){\r\n\t\t\t$this-&gt;Auth-&gt;setUser($user);\r\n\t\t\treturn $this-&gt;redirect($this-&gt;Auth-&gt;redirectUrl());\r\n\t\t}\r\n\t\t$this-&gt;Flash-&gt;error('\u30e6\u30fc\u30b6\u30fc\u540d\u304b\u30d1\u30b9\u30ef\u30fc\u30c9\u304c\u9593\u9055\u3063\u3066\u307e\u3063\u305b');\r\n\t}\r\n}<\/pre>\n<p>POST\u30e1\u30bd\u30c3\u30c9\u3067\u98db\u3093\u3067\u304d\u305f\u3089\u3001\u8a8d\u8a3c\u3092\u884c\u3046\u3002<br \/>\nOK\u3060\u3063\u305f\u3089\u3001Auth\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u306b\u3001\u30e6\u30fc\u30b6\u30fc\u3092\u30bb\u30c3\u30c8\u3057\u3066\u3001\u30ea\u30c0\u30a4\u30ec\u30af\u30c8\u3055\u305b\u308b\u3002<br \/>\n\u9593\u9055\u3063\u3066\u305f\u3089\u3001\u30d5\u30e9\u30c3\u30b7\u30e5\u30e1\u30c3\u30bb\u30fc\u30b8\u306b\u30a8\u30e9\u30fc\u5185\u5bb9\u3092\u66f8\u304f\u3002<\/p>\n<p>\u6b21\u306fTemplate\u3002<\/p>\n<p>Template\\Users\\login.ctp \u30d5\u30a1\u30a4\u30eb\u3092\u8ffd\u52a0\u3057\u3066\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u66f8\u304f\u3002<\/p>\n<pre class=\"lang:php decode:true \">&lt;h1&gt;Login&lt;\/h1&gt;\r\n&lt;?= $this-&gt;Form-&gt;create() ?&gt;\r\n&lt;?= $this-&gt;Form-&gt;input('email') ?&gt;\r\n&lt;?= $this-&gt;Form-&gt;input('password') ?&gt;\r\n&lt;?= $this-&gt;Form-&gt;button('Login') ?&gt;\r\n&lt;?= $this-&gt;Form-&gt;end() ?&gt;<\/pre>\n<p>\u3061\u306a\u307f\u306b\u3001Form\u30d8\u30eb\u30d1\u30fc\u3092\u4f7f\u3063\u3066\u66f8\u304f\u3068Form\u306e\u90e8\u5206\u306f\u3001\u4e0b\u8a18\u306e\u3088\u3046\u306aHTML\u306b\u306a\u308b\u304c\u3001\u5225\u306b\u30d8\u30eb\u30d1\u30fc\u4f7f\u308f\u305a\u306b\u76f4\u63a5\u66f8\u3044\u3066\u3082\u826f\u3044\u3002<\/p>\n<pre class=\"lang:xhtml decode:true \">&lt;form method=\"post\" accept-charset=\"utf-8\" action=\"\/users\/login\"&gt;\r\n  &lt;div style=\"display:none;\"&gt;\r\n    &lt;input type=\"hidden\" name=\"_method\" value=\"POST\"&gt;\r\n  &lt;\/div&gt;\r\n  &lt;div class=\"input email\"&gt;\r\n    &lt;label for=\"email\"&gt;Email&lt;\/label&gt;\r\n    &lt;input type=\"email\" name=\"email\" id=\"email\"&gt;\r\n  &lt;\/div&gt;\r\n  &lt;div class=\"input password\"&gt;\r\n    &lt;label for=\"password\"&gt;Password&lt;\/label&gt;\r\n    &lt;input type=\"password\" name=\"password\" id=\"password\"&gt;\r\n  &lt;\/div&gt;\r\n  &lt;button type=\"submit\"&gt;Login&lt;\/button&gt;\r\n&lt;\/form&gt;<\/pre>\n<p>\u3053\u308c\u3067\u3001\u30c8\u30c3\u30d7\u30da\u30fc\u30b8\u3067\u3082\u3001\u4f55\u3067\u3082\u826f\u3044\u306e\u3067\u30a2\u30af\u30bb\u30b9\u3059\u308b\u3068\u3001\u30ed\u30b0\u30a4\u30f3\u8a8d\u8a3c\u30da\u30fc\u30b8(\/users\/login)\u306b\u98db\u3070\u3055\u308c\u308b\u3002<\/p>\n<p><a href=\"http:\/\/gomocool.net\/gomokulog\/wp-content\/uploads\/2015\/08\/CakePHP3-3-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-665\" src=\"http:\/\/gomocool.net\/gomokulog\/wp-content\/uploads\/2015\/08\/CakePHP3-3-1.png\" alt=\"CakePHP3-3-1\" width=\"658\" height=\"369\" \/><\/a><\/p>\n<p>\u3053\u3053\u3067\u3001\u524d\u56de\u30d1\u30b9\u30ef\u30fc\u30c9\u30cf\u30c3\u30b7\u30e5\u5316\u3092\u884c\u3063\u305f\u30e6\u30fc\u30b6\u30fc\u3067\u30ed\u30b0\u30a4\u30f3\u3057\u3066\u307f\u308b\u3002<br \/>\n\u30ed\u30b0\u30a4\u30f3\u306b\u6210\u529f\u3059\u308b\u3068\u3001\u5165\u308d\u3046\u3068\u3057\u305f\u30da\u30fc\u30b8\u3078\u30ea\u30c0\u30a4\u30ec\u30af\u30c8\u3055\u308c\u308b\u3002\u9593\u9055\u3048\u308b\u3068\u3001\u8a2d\u5b9a\u3057\u305f\u30d5\u30e9\u30c3\u30b7\u30e5\u30e1\u30c3\u30bb\u30fc\u30b8\u304c\u8868\u793a\u3055\u308c\u308b\u3002<\/p>\n<p>\u3082\u3057\u3001<br \/>\n\u300c\u3042\u3001\u3084\u3063\u3079\u30e6\u30fc\u30b6\u30fc\u306a\u3093\u3060\u3063\u3051\uff1f\u30d1\u30b9\u30ef\u30fc\u30c9\u5fd8\u308c\u3061\u307e\u3063\u305f\u3002\u65b0\u3057\u304f\u4f5c\u308b\u304b\u3002\u300d<br \/>\n\u300c\u30e6\u30fc\u30b6\u30fc\u8ffd\u52a0\u30da\u30fc\u30b8 \/users\/add \u3078\u30a2\u30af\u30bb\u30b9\u3063\u3068\u3002\u300d<br \/>\n\u300c\u30c0\u30e1\u3060\u3002\/user\/login\u306b\u98db\u3070\u3055\u308c\u3066\u3057\u307e\u3046\u30fb\u30fb\u30fb\u3002\u300d\u3068\u306a\u3063\u305f\u5834\u5408\u306f\u3001UserController\u306b\u4e0b\u8a18\u3092\u8ffd\u52a0\u3059\u308b\u3002<\/p>\n<pre class=\"lang:php decode:true\">public function beforeFilter(\\Cake\\Event\\Event $event) {\r\n\tparent::beforeFilter($event);\r\n\t$this-&gt;Auth-&gt;allow(['add']);\r\n}<\/pre>\n<p>beforeFileter\u30e1\u30bd\u30c3\u30c9\u3092\u8ffd\u52a0\u3057\u3066\u3001\u305d\u3053\u306b\u3001<span class=\"lang:default decode:true  crayon-inline \">$this-&gt;Auth-&gt;allow([&#8216;add&#8217;]);<\/span> \u3068\u8a18\u8ff0\u3059\u308b\u3002<\/p>\n<p>\u3053\u308c\u306f\u3001UserController\u3067\u30a2\u30af\u30b7\u30e7\u30f3\u304c\u5b9f\u884c\u3055\u308c\u308b\u524d\u306badd\u30e1\u30bd\u30c3\u30c9\u306b\u95a2\u3057\u3066\u306f\u3001\u8a8d\u8a3c\u3092\u8a31\u53ef\u3057\u3066\u304a\u304f\u3088\u3002\u3068\u3044\u3046\u3053\u3068\u3002<\/p>\n<p>\u3053\u308c\u3067\u3001\/user\/add \u306b\u8a8d\u8a3c\u306a\u3057\u3067\u3064\u306a\u304c\u308b\u3088\u3046\u306b\u306a\u308b\u3002<\/p>\n<p>\u3061\u306a\u307f\u306b\u3001\u5f15\u6570\u306a\u3057\u3067\u3001<span class=\"lang:php decode:true crayon-inline\">$this-&gt;Auth-&gt;allow()<\/span>\u3068\u66f8\u304f\u3068\u3001\u5168\u30a2\u30af\u30b7\u30e7\u30f3\u304c\u8a31\u53ef\u306b\u306a\u308b\u3002<\/p>\n<p>\u30ed\u30b0\u30a2\u30a6\u30c8\u3082\u3064\u3051\u308b\u3002<\/p>\n<p>User\u30b3\u30f3\u30c8\u30ed\u30fc\u30e9\u30fc\u306b\u3001logout\u3092\u8ffd\u52a0<\/p>\n<pre class=\"lang:php decode:true\">\/**\r\n * \u30ed\u30b0\u30a2\u30a6\u30c8\r\n * @return type\r\n *\/\r\npublic function logout()\r\n{\r\n\t$this-&gt;Flash-&gt;success('\u30ed\u30b0\u30a2\u30a6\u30c8\u3057\u307e\u3057\u305f');\r\n\treturn $this-&gt;redirect($this-&gt;Auth-&gt;logout());\r\n}<\/pre>\n<p>\/user\/logout\u30da\u30fc\u30b8\u3078\u884c\u304f\u3068\u3001\u30ed\u30b0\u30a2\u30a6\u30c8\u304c\u5b9f\u884c\u3055\u308c\u3066\u3001\/user\/login\u30da\u30fc\u30b8\u3078\u30ea\u30c0\u30a4\u30ec\u30af\u30c8\u3055\u308c\u308b\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>\u8a8d\u8a3c\u306f\u901a\u3063\u305f\u3051\u3069\u3001\u8a8d\u8a3c\u3057\u305f\u30e6\u30fc\u30b6\u30fc\u306b\u3088\u3063\u3066\u3067\u304d\u308b\u4e8b\u3092\u5206\u3051\u305f\u3044\u3002<br \/>\n\u306a\u3069\u306e\u5834\u5408\u306b\u3001authorize\u3092\u4f7f\u7528\u3059\u308b\u3002<\/p>\n<p>\u307e\u305a\u3001Auth\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u306e\u521d\u671f\u5316\u306b\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u4e00\u884c\u8ffd\u52a0\u3059\u308b\u3002<\/p>\n<pre class=\"lang:php decode:true \">\/\/\u8a8d\u8a3c\r\n$this-&gt;loadComponent('Auth',[\r\n    'authorize' =&gt; 'Controller'  \/\/\u3053\u308c\u3092\u8ffd\u52a0\u3059\u308b\r\n    'authenticate' =&gt; [<\/pre>\n<p>\u3059\u308b\u3068\u3001\u8a8d\u8a3c\u3057\u305f\u30e6\u30fc\u30b6\u30fc\u306f\u3001isAuthorized\u3092\u901a\u308b\u3088\u3046\u306b\u306a\u308b\u306e\u3067\u3001\u4e0b\u8a18\u306e\u30e1\u30bd\u30c3\u30c9\u306e\u3088\u3046\u306a\u3082\u306e\u3092\u8ffd\u8a18\u3059\u308b\u3002<br \/>\n(\u4e0b\u8a18\u306f\u3042\u304f\u307e\u3067\u4f8b )<\/p>\n<pre class=\"lang:php decode:true \">public function isAuthorized($user = null){\r\n    $action = $this-&gt;request-&gt;params['action']\r\n    \/\/ \u4e00\u89a7\u3068\u8a73\u7d30\u306f\u8a8d\u8a3c\u3055\u3048\u901a\u308c\u3070\u3060\u308c\u3067\u3082\u8a31\u53ef\r\n    if (in_array($action, ['index','detail'])) {\r\n        return true;\r\n    }\r\n    \/\/ \u305d\u308c\u4ee5\u5916\u306f\u5f79\u5272\u304cadmin\u3060\u3063\u305f\u3089\u8a31\u53ef\r\n    if($user['role'] === 'admin')\r\n    {\r\n       return true;\r\n    }\r\n    \/\/ \u629c\u3051\u305f\u3082\u306e\u306f\u3068\u308a\u3042\u3048\u305a\u975e\u8a31\u53ef\r\n    return false;\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p>\u3061\u306a\u307f\u306b\u3001\u3053\u306e\u30da\u30fc\u30b8\u3060\u3051\u8a8d\u8a3c\u304b\u3051\u305f\u3044\u3063\u3066\u5834\u5408\u306f\u3001\u4e0b\u8a18\u306e\u3088\u3046\u306b\u6761\u4ef6\u3067\u5909\u3048\u305f\u308a\u3057\u3066\u3082\u3044\u3044\u304b\u3082\u3002<\/p>\n<pre class=\"lang:php decode:true\">if(\/\/\u306a\u306b\u304b\u30da\u30fc\u30b8\u306b\u3088\u308b\u6761\u4ef6){\r\n\t\/\/\u8a8d\u8a3c\r\n\t$this-&gt;loadComponent('Auth',[\r\n\t\t'authorize' =&gt; 'Controller',\r\n\t\t'authenticate' =&gt; [\r\n\t\t\t'Form' =&gt; [\r\n\t\t\t\t'fields' =&gt; [\r\n\t\t\t\t\t'username' =&gt; 'email',\r\n\t\t\t\t\t'password' =&gt; 'password'\r\n\t\t\t\t]\r\n\t\t\t]\r\n\t\t],\r\n\t\t'loginAction' =&gt; [\r\n\t\t\t'controller' =&gt; 'Users',\r\n\t\t\t'action' =&gt; 'login'\r\n\t\t]\r\n\t]);\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p>\u4ee5\u4e0a\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u30d5\u30a9\u30fc\u30e0(Form)\u306b\u3088\u308b\u8a8d\u8a3c\u6a5f\u80fd\u3092\u8ffd\u52a0\u3059\u308b\u3002 \u30e6\u30fc\u30b6\u30fc\u30c6\u30fc\u30d6\u30eb\u306f\u3001\u524d\u306e\u8a18\u4e8b\u3092\u53c2\u8003\u306b\u3002 AppController.php\u306einitialize\u30e1\u30bd\u30c3\u30c9\u306b\u8a8d\u8a3c\u95a2\u9023\u306e\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u3092\u8a2d\u5b9a\u3002 public function &hellip;  <a href=\"https:\/\/gomokulog.gomocool.net\/?p=664\" class=\"more-link\">more <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[56],"tags":[],"class_list":["post-664","post","type-post","status-publish","format-standard","hentry","category-cakephp3"],"_links":{"self":[{"href":"https:\/\/gomokulog.gomocool.net\/index.php?rest_route=\/wp\/v2\/posts\/664","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gomokulog.gomocool.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gomokulog.gomocool.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gomokulog.gomocool.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gomokulog.gomocool.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=664"}],"version-history":[{"count":3,"href":"https:\/\/gomokulog.gomocool.net\/index.php?rest_route=\/wp\/v2\/posts\/664\/revisions"}],"predecessor-version":[{"id":668,"href":"https:\/\/gomokulog.gomocool.net\/index.php?rest_route=\/wp\/v2\/posts\/664\/revisions\/668"}],"wp:attachment":[{"href":"https:\/\/gomokulog.gomocool.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=664"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gomokulog.gomocool.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=664"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gomokulog.gomocool.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=664"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}