cakephpwp用いたコメント機能を実装しようとしているのですがうまくいきません。
http://www.grooving-earth.com/cakephp-lesson/#19
このサイトのコメント機能実装を参考にしてやっています。
このサイトでは、
Comment.php
<?php class Comment extends AppModel { public $belongsTo = 'Post'; //全てのコメントはPostに帰属している。PostIdがあれば自動で紐づく }
Post.php
<?php class Post extends AppModel { public $hasMany = "Comment"; //Commentと紐づく public $validate = array ( 'title' => array ( 'rule' => 'notEmpty', 'message' => '記入してください。' // 表示するメッセージを指定 ), 'body' => array ( 'rule' => 'notEmpty' ) ); }
この2つを用いることでブラウザに下のようにコメント欄の表示ができるとなっているのですが、自分のブラウザには表示されません。
自分のブラウザはこのようになっています。
テーブルも、phpMyAdminを用いて作成しました。
参考にしているサイトのように表示させるにはどうしたらよいか教えていただきたいです。
どうかよろしくお願いします。
※追加
icchiさん、kunaiさんに言われた通りDBにID3、4、9のデータを追加したところ、SQLの方に変化はありましたが、表示されませんでした。
/cake_blog/posts/view/1にアクセスした結果
/cake_blog/posts/view/1を追加し、アクセスしたところ、以下のようなエラーが出ました。
それで、PostController.phpに表示されたコードを追加したところ、以下のようになりました。
変更したソースコード
PostsController.php
<?php App::uses('AppController', 'Controller'); /** * Posts Controller */ class PostsController extends AppController { /** 8~56行目追加*/ public $scaffold; public $helper = array('HTML', 'Form'); public function index() { $this->set('posts', $this->Post->find('all')); //$this->setでPosts変数へ代入。 $this->Post->fin('all')で記事を全て持ってくる。 } public $helper = array('HTML', 'Form'); public function index() { $this->set('posts', $this->Post->find('all')); //$this->setでPosts変数へ代入。 $this->Post->fin('all')で記事を全て持ってくる。 $this->set('title_for_layout', '記事一覧'); //タイトルをセット } public $helper = array('HTML', 'Form'); public function index() { $param = array ( 'order' => 'modified desc', 'limit' => 2 ); $this->set('posts', $this->Post->find('all', $param)); //$this->setでPosts変数へ代入。 $this->Post->fin('all')で記事を全て持ってくる。 $this->set('title_for_layout', '記事一覧'); //タイトルをセット } public $helper = array('HTML', 'Form'); public function index() { $this->set('posts', $this->Post->find('all')); //$this->setでPosts変数へ代入。 $this->Post->fin('all')で記事を全て持ってくる。 $this->set('title_for_layout', '記事一覧'); //タイトルをセット } public function view($id = null) { $this->Post->id = $id; $this->set('post', $this->Post->read()); } public function add() { if ($this->request->is('post')) { if ($this->Post->save($this->request->data)) { $this->Session->setFlash('Success!'); $this->redirect(array('action' => 'index')); } else { $this->Session->setFlash('failed'); } } } /** * Scaffold * * @var mixed */ //public $scaffold; public function index(){ $posts = $this->Post->find('all'); $this->set('posts', $posts); } public function view() { } } ?>
add.ctp
<h2>Add post</h2> <?php echo $this->Form->create('Post'); echo $this->Form->input('title'); echo $this->Form->input('body', array('rows'=>3)); echo $this->Form->end('Save Post');
index.ctp
<?php //print_r($posts); foreach($posts as $post){ echo $post['Post']['title']."<br>\n"; echo $post['Post']['body']."<br>\n"; # code... } ?> <ul> <?php foreach ($posts as $post) : ?> <li> <?php debug($post); ?> </li> <?php endforeach; ?> </ul> </address> <h2>記事一覧</h2> <ul> <?php foreach ($posts as $post) : ?> <li> <?php // debug($post); echo h($post['Post']['title']); //hとはCakePHPが持っているHTML Special charactoresの省略形 ?> </li> <?php endforeach; ?> </ul> <h2>Add Post</h2> <?php echo $this->Html->link('Add post', array('controller' => 'posts', 'action' => 'add')); ?>
routes.php
<?php /** * Routes configuration * * In this file, you set up routes to your controllers and their actions. * Routes are very important mechanism that allows you to freely connect * different urls to chosen controllers and their actions (functions). * * PHP 5 * * CakePHP(tm) : Rapid Development Framework (http://cakephp.org) * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) * * Licensed under The MIT License * For full copyright and license information, please see the LICENSE.txt * Redistributions of files must retain the above copyright notice. * * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) * @link http://cakephp.org CakePHP(tm) Project * @package app.Config * @since CakePHP(tm) v 0.2.9 * @license http://www.opensource.org/licenses/mit-license.php MIT License */ /** * Here, we are connecting '/' (base path) to controller called 'Pages', * its action called 'display', and we pass a param to select the view file * to use (in this case, /app/View/Pages/home.ctp)... */ Router::connect('/', array('controller' => 'posts', 'action' => 'index'));/**追加*/ Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home')); /** * ...and connect the rest of 'Pages' controller's urls. */ Router::connect('/pages/*', array('controller' => 'pages', 'action' => 'display')); /** * Load all plugin routes. See the CakePlugin documentation on * how to customize the loading of plugin routes. */ CakePlugin::routes(); /** * Load the CakePHP default routes. Only remove this if you do not want to use * the built-in default routes. */ require CAKE . 'Config' . DS . 'routes.php';
default.ctp
<?php /** * * PHP 5 * * CakePHP(tm) : Rapid Development Framework (http://cakephp.org) * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) * * Licensed under The MIT License * For full copyright and license information, please see the LICENSE.txt * Redistributions of files must retain the above copyright notice. * * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) * @link http://cakephp.org CakePHP(tm) Project * @package app.View.Layouts * @since CakePHP(tm) v 0.10.0.1076 * @license http://www.opensource.org/licenses/mit-license.php MIT License */ <title> <?php echo $title_for_layout; ?> </title> <div id="header"> <h1><?php echo $this->Html->link('Home', '/'); ?></h1> </div> $cakeDescription = __d('cake_dev', 'CakePHP: the rapid development php framework'); ?> <!DOCTYPE html> <html> <head> <?php echo $this->Html->charset(); ?> <title> <?php echo $cakeDescription ?>: <?php echo $title_for_layout; ?> </title> <?php echo $this->Html->meta('icon'); echo $this->Html->css('cake.generic'); echo $this->fetch('meta'); echo $this->fetch('css'); echo $this->fetch('script'); ?> </head> <body> <div id="container"> <div id="header"> <h1><?php echo $this->Html->link($cakeDescription, 'http://cakephp.org'); ?></h1> </div> <div id="content"> <?php echo $this->Session->flash(); ?> <?php echo $this->fetch('content'); ?> </div> <div id="footer"> <?php echo $this->Html->link( $this->Html->image('cake.power.gif', array('alt' => $cakeDescription, 'border' => '0')), 'http://www.cakephp.org/', array('target' => '_blank', 'escape' => false) ); ?> </div> </div> <?php echo $this->element('sql_dump'); ?> </body> </html>
view.ctp
<h2><?php echo h($post['Post']['title']); ?></h2> <p><?php echo h($post['Post']['body']); ?></p> <h2>Comments</h2> <ul> <?php foreach ($post['Comments'] as $comment) : ?> <li> <?php echo h($comment['body']); ?> </li> <?php endforeach; ?> </ul>
これを実装した際に出たエラー画像
(/cake_blog/posts/view/1を追加した際に出たエラー)
CakeBlogController.phpの作成を命じられたエラーの画像
上の2つのコードを実装してみたあとのエラーの画像(viewに関して)
icchiさん修正前のview.ctpを実装した際のブラウザの画像(エラー含む)
](7eef62bc967560326603bd36e95c7442.png)
icchiさん修正後のview.ctpを実装した際のブラウザ
の画像(エラー含む)
修正前と修正後両方を足したview.ctpを実装した際のブラウザ
の画像(エラー含む)
修正前の実装で出たエラーFormHelper.phpのコードを載せようとしたところ、字数オーバーになってしまったので、エラーに関する行を載せさせていただきます。
その行だけではわからないという場合は教えてください。
FormHelper.phpのエラーに関するソースコード
if (isset($options['action'])) { trigger_error('Using key `action` is deprecated, use `url` directly instead.', E_USER_DEPRECATED); }
triggerからDEPRECATED);までがエラー
対象の383行目です。

回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/09/06 08:09
2016/09/06 08:32 編集
2016/09/06 09:31
2016/09/06 09:48 編集
2016/09/06 10:45
2016/09/06 10:45
2016/09/06 10:49
2016/09/06 11:48
2016/09/06 13:09
2016/09/06 13:26
2016/09/06 13:29
2016/09/06 13:45 編集
2016/09/07 05:46
2016/09/07 06:20
2016/09/07 08:13
2016/09/07 08:54
2016/09/07 09:01
2016/09/07 09:54
2016/09/07 11:25 編集
2016/09/07 13:03
2016/09/07 13:04
2016/09/07 13:04
2016/09/07 13:25
2016/09/07 13:31
2016/09/07 13:34
2016/09/07 13:36
2016/09/07 14:33 編集
2016/09/08 06:21
2016/09/08 06:30