Home >Backend Development >PHP Tutorial >yii2中a连接用post方法请求action却执行了三遍

yii2中a连接用post方法请求action却执行了三遍

WBOY
WBOYOriginal
2016-06-06 20:27:491245browse

现在情况是,我点了一下a链接,是 post 请求,参数是 get传递的, actionTripDays 方法执行了三遍,表里被插入了三条数据,应该只执行一次,插入一条数据的,这是为毛呢??求帮助

但我在,if 里,dump($position) 却只有一次输出。

<code><a href="<?=%20Url::toRoute(%5B'trip-days',%20'id'%20=>%20%24logId,%20'position'%20=>%20'end'%5D)%20?>" class="add-one-day" data-method="post" rel="nofollow">+ 新增后一天</a>


public function actionTripDays($id)
{
    $model = $this->findModel($id);

    // 最开始是这样写的,执行了两遍
    if (!empty($position = Yii::$app->request->get('position', '')))
 
    // 变成下面这样干脆执行了三遍
    if (Yii::$app->request->getIsPost() && !empty($position = Yii::$app->request->get('position', ''))) {
        // dump(Yii::$app->request->get('position', ''));
        // exit;
        TravelDay::addDay($position, $model->id);
    } else {
        TravelDay::initDay($model->id);
    }


    $days = TravelDay::find()
                ->where(['travel_log_id' => $model->id])
                ->orderBy(['order' => SORT_ASC])
                ->all();

    $images = TravelImg::find()
            ->where(['travel_log_id' => $model->id])
            ->orderBy(['order' => SORT_ASC])
            ->all();


    return $this->render('trip-days', [
            'images' => $images,
            'days' => $days,
            'logId' => $model->id,
        ]);
}</code>

回复内容:

现在情况是,我点了一下a链接,是 post 请求,参数是 get传递的, actionTripDays 方法执行了三遍,表里被插入了三条数据,应该只执行一次,插入一条数据的,这是为毛呢??求帮助

但我在,if 里,dump($position) 却只有一次输出。

<code><a href="<?=%20Url::toRoute(%5B'trip-days',%20'id'%20=>%20%24logId,%20'position'%20=>%20'end'%5D)%20?>" class="add-one-day" data-method="post" rel="nofollow">+ 新增后一天</a>


public function actionTripDays($id)
{
    $model = $this->findModel($id);

    // 最开始是这样写的,执行了两遍
    if (!empty($position = Yii::$app->request->get('position', '')))
 
    // 变成下面这样干脆执行了三遍
    if (Yii::$app->request->getIsPost() && !empty($position = Yii::$app->request->get('position', ''))) {
        // dump(Yii::$app->request->get('position', ''));
        // exit;
        TravelDay::addDay($position, $model->id);
    } else {
        TravelDay::initDay($model->id);
    }


    $days = TravelDay::find()
                ->where(['travel_log_id' => $model->id])
                ->orderBy(['order' => SORT_ASC])
                ->all();

    $images = TravelImg::find()
            ->where(['travel_log_id' => $model->id])
            ->orderBy(['order' => SORT_ASC])
            ->all();


    return $this->render('trip-days', [
            'images' => $images,
            'days' => $days,
            'logId' => $model->id,
        ]);
}</code>

我不是来回答,我是想问 yii 是怎么实现给a元素设定请求方式的

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn