php的yii中的一个页面跳转问题

目前如果处理不被通过的时候错误信息等等均出现在同一个页面中,接下来试图创建新的页面文件并在处理不被通过时带着错误信息跳转新的页面,设置如下:

在"protected/views/abcd/efg/needbranchshopcancel.tpl"路径下创建"needbranchshopcancel.tpl"文件,并设定main.php

main.php的一部分

'urlManager'=>array(
            'urlFormat'=>'path',
            'rules'=>array(
                '' => '/site/index',
            
                .....

                'abcd/efg/needbranchshopcancel' => 'abcd/efg/needbranchshopcancel',
            ),
            'showScriptName' => false,
        ),

转移needbranchshopcancel.tpl文件前面的页面部分

} else if (!empty($shop['shopBranch'])) {
                    $error_message = self::SHOP_INFORMATION_ALERT;
                    $branch_exists = self::BRANCH_ERROR_MESSAGE;
                    $branch_list = $shop['shopBranch'];
                    $validate = true;
                    // 若处理被阻止
                    $this->template = 'abcd/efg/needbranchshopcancel';

出现了一个问题,似乎是实现了跳转(needbranchshopcancel里的内容被显示),但是浏览器上的域名显示并没有被改变
例如跳转前是"https://www.catcat/abc/def"
跳转后依然是"https://www.catcat/abc/def"而不是""https://www.catcat/abc/def/needbranchshopcancel"

难道是哪里出了问题或许是理解不到位,
感谢指导!

根据代码字面意思应该是动态页面模板为'abcd/efg/needbranchshopcancel',并非执行跳转操作。

$this->template = 'abcd/efg/needbranchshopcancel'

而urlManager中rules是指定url重新规则,并不是要做跳转,要做跳转用redirect跳转到rules定义的页面


$this->redirect(array('abcd/efg/needbranchshopcancel'));

该回答引用ChatGPT4与博主@晓码自在合作编写:

这个问题是因为你的urlManager规则中,为needbranchshopcancel指定的url格式是:


php
'abcd/efg/needbranchshopcancel' => 'abcd/efg/needbranchshopcancel',

这意味着当访问abcd/efg/needbranchshopcancel路径时,Yii会显示abcd/efg/needbranchshopcancel视图。

但是,这并不会实际改变浏览器的URL - 浏览器URL仍然显示abcd/efg/needbranchshopcancel。

要实现真正的URL跳转和视图切换,你需要在urlManager中定义一个别名,并指向needbranchshopcancel视图。例如:

php
'abcd/efg/needbranchshopcancel' => 'site/needbranchshopcancel',

这意味着当访问abcd/efg/needbranchshopcancel路径时,Yii会调用site控制器的needbranchshopcancel动作,并显示needbranchshopcancel视图。

这时,浏览器URL也会变为/site/needbranchshopcancel,实现真正的URL跳转。

所以,要修复这个问题,你需要:

  1. 在urlManager中,为needbranchshopcancel路径定义一个别名,例如:
php 
'abcd/efg/needbranchshopcancel' => 'site/needbranchshopcancel',

  1. 在site控制器中定义needbranchshopcancel动作
php
public function actionNeedbranchshopcancel()
{
    $this->render('needbranchshopcancel'); 
}

  1. needbranchshopcancel视图保持不变。

  2. 跳转时,使用URL helper生成该别名对应的URL,例如:

php 
$this->redirect(['site/needbranchshopcancel']);

这样,当你访问该跳转链接时,浏览器URL会变为/site/needbranchshopcancel,并显示needbranchshopcancel视图,实现真正的URL重定向和视图切换。

您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!
PS:问答VIP年卡 【限时加赠:IT技术图书免费领】,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632