douzhao7634
2015-12-26 11:15
浏览 53
已采纳

在actionView yii2中从url获取id值的问题

I'm trying to pass the id from the URL using the get method, but when I try to get it with actionView in the controllers. When I try to echo the id to check, if it passes the correct id from the URL. the value is always my controller ID. here is my view which passes the id when I click on delete button :

<?php
  foreach($twit as $twits){
?>


<header class="text-right">
    <div class="comment-user"> سبحان باقری <i class="fa fa-user"></i></div>
    <time class="comment-date" datetime="16-12-2014 01:05"> Dec 16, 2014 <i class="fa fa-clock-o"></i></time>
</header>
<div class="panel panel-default arrow left">

  <div class="panel-body">

    <?= Html::a('Delete','?r=twit/delete-twit&id='.$twits['id'],['class' => 'pull-left btn btn-danger btn-sm']); ?>
    <?= Html::a('Update','#&id='.$twits['id'],['class' => 'pull-left btn btn-primary btn-sm']); ?>


      <div class="comment-post">
          <p style="text-align:right;font-weight:bold;">
              <?php echo $twits['twit']; ?>
          </p>
      </div>
      <p class="text-right"><a href="#" class="btn btn-success btn-sm"><i class="fa fa-reply"></i> reply</a></p>
  </div>
</div>

<?php  } ?>

and here is my controller actionView which i want to get the id value and then send it to actionDeleteTwit :

public function actionView($id = null)
{
    echo $id;
    die();
}

and here is my actionDeleteTwit which i don't know how to pass the id value to it :

public function actionDeleteTwit()
{

    if(Yii::$app->request->get() && Yii::$app->request->get('id')){
        $id = Yii::$app->request->get('id');
        Twit::deleteTwit($id);
        \yii::$app->response->redirect('?r=twit/index',301)->send();
    }
}

(I don't want to use the Yii::$app->request->get('id') and I'm trying to use the actionView instead )

图片转代码服务由CSDN问答提供 功能建议

我正在尝试使用get方法从URL传递id,但是当我尝试使用get方法传递它时 控制器中的actionView。 当我尝试回显要检查的id时,如果它从URL传递了正确的id。 值始终是我的控制器ID。 这是我点击删除按钮时传递id的视图:

 &lt;?php 
 foreach($ twit as $ twits){
?&gt; \  n 
 
&lt; header class =“text-right”&gt; 
&lt; div class =“comment-user”&gt;  سبحانباقری&lt; i class =“fa fa-user”&gt;&lt; / i&gt;&lt; / div&gt; 
&lt; time class =“comment-date”datetime =“16-12-2014 01:05”&gt  ;  2014年12月16日&lt; i class =“fa fa-clock-o”&gt;&lt; / i&gt;&lt; / time&gt; 
&lt; / header&gt; 
&lt; div class =“panel panel-default arrow left”&gt  ; 
 
&lt; div class =“panel-body”&gt; 
 
&lt;?= Html :: a('删除','?r = twit / delete-twit&amp; id ='。$ twits  ['id'],['class'=&gt;'pull-left btn btn-danger btn-sm']);  ?&gt; 
&lt;?= Html :: a('更新','#&amp; id ='。$ twits ['id'],['class'=&gt;'pull-left btn btn-primary btn  -sm']);  ?&gt; 
 
 
&lt; div class =“comment-post”&gt; 
&lt; p style =“text-align:right; font-weight:bold;”&gt; 
&lt;?php  echo $ twits ['twit'];  ?&gt; 
&lt; / p&gt; 
&lt; / div&gt; 
&lt; p class =“text-right”&gt;&lt; a href =“#”class =“btn btn-success btn-sm”  &gt;&lt; i class =“fa fa-reply”&gt;&lt; / i&gt; 回复&lt; / a&gt;&lt; / p&gt; 
&lt; / div&gt; 
&lt; / div&gt; 
 
&lt;?php}?&gt; 
   
 
 

这是我的控制器actionView,我想获取id值,然后将其发送到actionDeleteTwit:

  public function actionView($ id = null)
 {
  echo $ id; 
 die(); 
} 
   
 
 

这是我的actionDeleteTwit,我不知道如何将id值传递给它:

  public function actionDeleteTwit()
 {
 
 if if(Yii :: $ app-&gt; request-&gt; get()&amp;&amp; Yii ::  $ app-&gt; request-&gt; get('id')){
 $ id = Yii :: $ app-&gt; request-&gt; get('id'); 
 Twit :: deleteTwit($ id  ); 
 \ yii :: $ app-&gt; response-&gt; redirect('?r = twit / index',301) - &gt; send(); 
} 
} 
  <  / pre> 
 
 

(我不想使用 Yii :: $ app-&gt; request-&gt; get('id')而我正在尝试 改为使用actionView

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

  • dqstti8945 2015-12-26 12:14
    最佳回答

    You must change you delete action to a thing like this:

    public function actionDeleteTwit($id = null)
    {
        if(null != $id){
            Twit::deleteTwit($id);
            \yii::$app->response->redirect('?r=twit/index',301)->send();
        }
    }
    

    As you used this notation in your view action. If more help needed, more information from your problem is needed.

    Update: You must pass your numeric values in get method (Or more general based on your routing table, your other parameters) as a part of url, i.e. your view file must be like this:

    <?php
      foreach($twit as $twits){
    ?>
    
    
    <header class="text-right">
        <div class="comment-user"> سبحان باقری <i class="fa fa-user"></i></div>
        <time class="comment-date" datetime="16-12-2014 01:05"> Dec 16, 2014 <i class="fa fa-clock-o"></i></time>
    </header>
    <div class="panel panel-default arrow left">
    
      <div class="panel-body">
    
        <?= Html::a('Delete',['/twit/delete-twit', 'id' => $twits['id']],['class' => 'pull-left btn btn-danger btn-sm']); ?>
        <?= Html::a('Update','#&id='.$twits['id'],['class' => 'pull-left btn btn-primary btn-sm']); ?>
    
    
          <div class="comment-post">
              <p style="text-align:right;font-weight:bold;">
                  <?php echo $twits['twit']; ?>
              </p>
          </div>
          <p class="text-right"><a href="#" class="btn btn-success btn-sm"><i class="fa fa-reply"></i> reply</a></p>
      </div>
    </div>
    
    <?php  } ?>
    

    Please consider Yii2 Guide - Action Parameters for more information.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题