初めまして、よろしくお願いします。
upload pluginで投稿した画像をviewで表示させようと思い、
下記のサイトの"既存テーブルにカラムを追加する場合"という項目を参考に作業を進めていました。
しっかり理解する CakePHP Upload プラグインの使い方 (1)
しっかり理解する CakePHP Upload プラグインの使い方 (2)
最初cakephp2の公式チュートリアルにあるbakeを使って作るブログアプリをベースに作業しており、それをベースに上記のサイトを参考にしてupload pluginを導入したら問題なく動作しposts/viewにも画像の表示をさせることが出来たのですが、
bakeで作ってしまったため理解が追いついてなく、またコードが見にくかったため、理解を深めるために公式チュートリアルにある、bakeを使わないで作る方で一から作り直してまたupload pluginを入れようとしました。
画像の投稿自体は問題なくいき、webrootの中にもちゃんと画像が格納されるのですが、posts/viewに画像を表示させることが出来なくて詰まってしまいました。
基本的に最初にやったことと同じようにやったのですが、おそらく何かの記述が足りないような気がしたので、一通り確認してみましたが上手くいかないため質問させて頂きました。
上にあるサイトの既存テーブルにカラムを追加する場合のところではUsersテーブルにカラムを追加していますが、新規ポストを投稿する際にタイトルと本文と一緒に画像を投稿したかったのでpostsテーブルにカラムを追加してやっております。
すいませんが、ご教授よろしくお願いします。
Model/post.php
class Post extends AppModel { public $actsAs = array( 'Search.Searchable',"Upload.Upload" => array( "photo" => array( "fields" => array( "dir" => "photo_dir", ), ), ), ); public $filterArgs = array( 'title' => array('type' => 'like'), ); public $validate = array( 'title' => array( 'rule' => 'notBlank' ), ); public function isOwnedBy($post, $user) { return $this->field('id', array('id' => $post, 'user_id' => $user)) !== false; } } ?>
View/Posts/add.ctp
<h1>記事の追加</h1> <?php echo $this->Form->create('Post', array('type' => 'file')); echo $this->Form->input('title'); echo $this->Form->input('body', array('rows' => '3')); //uploadプラグイン echo $this->Form->input('Post.photo', array('type' => 'file')); echo $this->Form->input('Post.photo_dir', array('type' => 'hidden')); echo $this->Form->end('投稿'); ?>
View/Posts/view.ctp
<h1>-- タイトル --</h1> <p><?php echo h($post['Post']['title']); ?><p> <h1>-- 本文 --</h1> <!-- 更新日時 --> <?php echo $post['Post']['created']; ?> <p><?php echo h($post['Post']['body']); ?></p> <!-- Uploadプラグイン --> <?php $post = 0; $base = $this->Html->url( "/files/image" ); $this->Html->image( $base . $post["photo_dir"] . "/" . $post["photo"] ); ?>
PostsController.php
<?php class PostsController extends AppController { public $uses = array('Post','User'); public $helpers = array('Html', 'Form'); public $components = array('Search.Prg'); public $presetVars = true; public function index() { //searchプラグイン $this->Prg->commonProcess(); $this->paginate = array( 'conditions' => $this->Post->parseCriteria($this->passedArgs), ); // $this->set('posts', $this->Post->find('all')); } public function view($id = null) { if (!$id) { throw new NotFoundException(__('Invalid post')); } $post = $this->Post->findById($id); if (!$post) { throw new NotFoundException(__('Invalid post')); } $this->set('post', $post); } public function add() { if ($this->request->is('post')) { $this->Post->create(); if ($this->Post->saveall($this->request->data)) { $this->Flash->success(__('記事を投稿しました')); return $this->redirect(array('action' => 'index')); } $this->Flash->error(__('記事を投稿出来ませんでした')); } } public function edit($id = null) { if (!$id) { throw new NotFoundException(__('Invalid post')); } $post = $this->Post->findById($id); if (!$post) { throw new NotFoundException(__('Invalid post')); } if ($this->request->is(array('post', 'put'))) { $this->Post->id = $id; if ($this->Post->save($this->request->data)) { $this->Flash->success(__('記事の更新をしました')); return $this->redirect(array('action' => 'index')); } $this->Flash->error(__('記事を更新出来ませんでした')); } if (!$this->request->data) { $this->request->data = $post; } } public function delete($id) { if ($this->request->is('get')) { throw new MethodNotAllowedException(); } if ($this->Post->delete($id)) { $this->Flash->success( __('記事を削除しました', h($id)) ); } else { $this->Flash->error( __('記事を削除出来ませんでした', h($id)) ); } return $this->redirect(array('action' => 'index')); } }
画像の格納先はwebroot/files/imageの中に指定しています。
回答1件
あなたの回答
tips
プレビュー