【前提条件(環境)】
CakePHP 3.0
【現状(今できていること)】
現在SNSの仕組みをCakePHPで構築中でございます。
できていることとして、ログインしたユーザーで記事を投稿し、
その記事を表示させる仕組みは作れました。
ただ別のユーザーでログインしてそのユーザーでも記事は投稿できるのですが、
全ての投稿を表示させることまでしか、どうしてもできません。
【要件(したいこと)】
SNSの個人ページみたいに、ログインユーザーのみの記事だけを表示させたい。
どうやって判定させて実現させていくのかの糸口がつかめず、停滞中で困っております。
誰か詳しい方ご教授いただけないでしょうか?
html
1ビュー 2src/Template/Contents/index.ctp 3 4<?php 5// ログイン認証判定 6$user_login_id = $this->request->session()->read('Auth.User.id'); 7?> 8<div class="UsersForm"> 9 <div class="form"> 10 <?= $this->Form->create($data,['type'=>'post','url'=>['action'=>'add','type'=>'file']]) ?> 11 <legend>今なにしてる?</legend> 12 <?= $this->Form->hidden('Contents.user_id',['type'=>'text','value'=>$user_login_id]);?> 13 14 <?= $this->Form->input('Contents.title',['type' => 'text','placeholder'=>'20文字以内でタイトルを入れてね','label' => '']) ?> 15 <?= $this->Form->input( 'Contents.body', ['type' => 'textarea','cols' => 10,'rows' => 7,'placeholder'=>'140文字以内でコメントしてね','label' => '' ] ); ?> 16 <?= $this->Form->button('投稿する'); ?> 17 <?= $this->Form->end() ?> 18 </div> 19 20 <div class="content"> 21 <?php foreach($data as $obj): ?> 22 <div class="post"> 23 <div class="post_name"><?= h($obj['user']['username']) ?>さんから投稿</div> 24 <div class="post_title"><?= h($obj->id) ?> : <?= h($obj->title) ?></div> 25 <div class="post_body"><?= h($obj->body) ?></div> 26 <?= $this->Form->create(null,['type'=>'post','url'=>['action'=>'delRecord']]) ?> 27 <?= $this->Form->hidden('id',['type' => 'text','placeholder'=>'削除した記事のIDを入力','label' => '','value'=>h($obj->id)]) ?> 28 <?= $this->Form->submit('削除') ?> 29 <?= $this->Form->end() ?> 30 </div> 31 <?php endforeach; ?> 32 </div> 33 34 <div class="paginator"> 35 <ul class="pagination"> 36 <?= $this->Paginator->numbers([ 37 'before'=>$this->Paginator->first('< 最初').' ', 38 'after'=>' '.$this->Paginator->last('> 最後'), 39 'modulus'=>9, 40 'separator'=>'・' 41 ]) ?> 42 </ul> 43 </div> 44</div>
php
1コントローラー 2src/Controller/ContentsController.php 3 4<?php 5namespace App\Controller; 6 7use App\Controller\AppController; 8use Cake\Event\Event; 9use Cake\Filesystem\Folder; 10use Cake\Filesystem\File; 11use RuntimeException; 12use Cake\ORM\TableRegistry; 13use \Exception; 14use Cake\Log\Log; 15 16class ContentsController extends AppController{ 17 18 private $users; 19 public $paginate = [ 20 'limit' => 5, 21 'order' => [ 22 'id'=> 'DESC' 23 ], 24 'contain' => ['Users'] 25 ]; 26 27 public function initialize(){ 28 parent::initialize(); 29 $this->users = TableRegistry::get('Users'); 30 $this->set('header','User/header'); 31 $this->set('footer','User/footer'); 32 $this->viewBuilder()->autoLayout(true); 33 $this->viewBuilder()->Layout('content'); 34 } 35 36 public function index(){ 37 $data = $this->paginate($this->Contents); 38 $this->set('data',$data); 39 $this->set('count',$data->count()); 40 } 41 42 public function add(){ 43 44 $content = $this->Contents->newEntity(); 45 if ($this->request->isPost()) { 46 $PostData = $this->request->data(); 47 $content = $this->Contents->patchEntity($content, $PostData); 48 if ($this->Contents->save($content)) { 49 return $this->redirect(['action' => 'index']); 50 }else{ 51 return $this->redirect(['action' => 'index']); 52 } 53 } 54 } 55 56 public function delRecord(){ 57 if($this->request->isPost()){ 58 try{ 59 $entity = $this->Contents->get($this->request->data['id']); 60 $this->Contents->delete($entity); 61 }catch(Exception $e){ 62 Log::write('debug',$e->getMessage()); 63 } 64 } 65 66 $this->redirect(['action' => 'index']); 67 } 68 69 // アクセス権限 70 public function isAuthorized($user = null){ 71 $action = $this->request->params['action']; 72 73 if(in_array($action,['index','view'])){ 74 return true; 75 } 76 if($user['role'] === 'admin'){ 77 return true; 78 } 79 if($user['role'] === 'author'){ 80 return true; 81 } 82 } 83 84} 85 86?>

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/05/07 12:00
2018/05/07 12:36
2018/05/07 12:42