2013-07-31 78 views
0

我有一個帖子索引視圖調用一個元素的內容輸入字段進行評論。登錄後調用一個元素

我把它用這種方式

<?php echo $this->element('addcomment', array('post_id' => $post['Post']['id'])); ?> 

這項工作很好,我傳遞的帖子ID參數,在addcomment元素,因爲POST_ID輸入字段隱藏在addcomment。當然我不希望用戶輸入帖子ID。

我已經設置了授權機制,以允許向標識(已連接)的用戶添加評論。

當未連接的用戶嘗試添加評論時,他會收到登錄屏幕。

登錄後,他被重定向到添加提交表單。問題在於,它同時失去了post_id變量的值。

Rem:如果在向帖子添加評論之前連接了用戶,它就會起作用。

如果我的解釋不清楚或者您需要更多信息,請不要猶豫與我聯繫。

這是我addcomment元素

<div class="Addcomment form"> 
<?php 
echo $this->Form->create('Comment', array(
    'url' => array('controller' => 'comments', 'action' => 'add') 
)); ?> 

    <fieldset> 
     <legend><?php echo __('Add Comment'); ?></legend> 


    <?php if (isset($current_user['id']) && isset($post_id)): ?> 

      <?php $this->request->data['Comment']['user_id'] = $current_user['id']; ?> 
      <?php $this->request->data['Comment']['post_id'] = $post_id; ?> 
      <?php echo $this->Form->input('post_id', array('type' => 'hidden')); ?> 
      <?php echo $this->Form->input('user_id', array('type' => 'hidden')); ?> 
    <?php else: echo $this->Form->input('post_id'); ?> 
    <?php endif; ?> 

    <?php echo $this->Form->input('content', array('class' => 'comment')); ?> 


    </fieldset> 
<?php echo $this->Form->end(__('Submit')); ?> 
</div> 

回答

0

按我理解您的問題。您可以使用會話變量來存儲帖子ID,如果用戶登錄,該帖子不應該受到影響。使用此會話後,您可以在登錄後將用戶重定向到特定帖子。

  1. 在將用戶重定向到登錄之前設置會話值。

    $ this-> Session-> write('last_post_id',YOUR_POST_ID);

  2. 如果用戶成功登錄並且如果發現會話值不爲空,則將用戶重定向到帖子。

    如果($這個 - >會話級>檢查( 'last_post_id')& & $這個 - >會話級>閱讀( 'last_post_id')!= ''){$ 這 - >重定向(YOUR_URL_TO_POST。「 ?postid ='。$ this-> Session-> read('last_post_id')); 退出; }其他{// 重定向的師範}

希望這會幫助你。重定向後,如果不再需要,請重置會話last_post_id。

相關問題