dongmei2956 2017-08-14 06:32
浏览 154
已采纳

获取yii2中重定向请求的URL?

Sorry for my English, but what I'm trying to say is explained below.

I have a controller say ControllerCard which has an action like this.

function actionScanCard()
{
    ...
    $this->redirect('/transaction/redeem');
    ...
}

In other controllers, ControllerTransaction, I am trying to get that it comes/redirected from /card/scan-card

function actionRedeem()
{
    $redirectFrom = ????;
    if ($redirectFrom === '/card/scan-card')
    {
        // some actions
    }
    else
        throw new ForbiddenHttpException('Must scan card!');
}

How do I get this $redirectFrom value with Yii2?

  • 写回答

1条回答 默认 最新

  • dongliuzi3410 2017-08-14 08:14
    关注

    You could use the remember() & previous() methods in yii\helpers\BaseUrl.

    function actionScanCard()
    {
        ...
        \yii\helpers\Url::remember();
        $this->redirect('/transaction/redeem');
        ...
    }
    

    in TransactionController (or other)

    function actionRedeem()
    {
        $url = \yii\helpers\Url::previous();
        if($url === Url::to('card/scan-card')) {
            // some actions
        } else{}
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 不懂得怎么运行下载来的代码
  • ¥15 CST导出3D模型图为什么和软件显示不一样?
  • ¥15 加热反应炉PLC控制系统设计(相关搜索:梯形图)
  • ¥15 python 用Dorc包报错,我的写法和网上教的是一样的但是它显示无效参数,是什么问题
  • ¥15 经过滑动平均后的一维信号还原用什么结构好呢?
  • ¥15 指定IP电脑的访问设置
  • ¥30 matlab ode45 未发现警告,但是运行出错
  • ¥15 为什么devc++编译项目会失败啊
  • ¥15 vscode platformio
  • ¥15 代写uni代码,app唤醒