dtz46697 2013-04-30 06:42
浏览 8
已采纳

在Yii框架中使用哪种URL操作更好

I was wandering which solution is better to use in Yii framework,

1) Redirecting page from somethingController.php

$this->redirect(array($this->id."/something"));

|| or

2) Creating Url

$this->createUrl($this->id."/something");

in view using contoroller & action you need.

Or maybe there is a better solution?

Thanks

  • 写回答

2条回答 默认 最新

  • doupeng5320 2013-04-30 06:59
    关注

    It like ask what is better miles or pounds?. That functions are very different.

    You need to use redirect when need to change page without user action in some conditions, for example in controller:

    if($money==0)
    {
        $this->redirect(array('alerts/notEnoughMoney'));
    }
    

    If you want to generate address what will used for example in html links, then you need to use createUrl, because it will:

    • Avoid unnecessary step with redirect
    • Better for SEO, and will be more user friendly
    • Better for customizing

    You can use createUrl in view, for example:

    <?php
    $link = $this->createUrl(array('user/profile'));
    ?>
    
    <a href="<?php echo $link ?>">My Profile</a>
    

    In any case, if you using redirects what visible for search bots you need to add second parameter:

    $this->redirect(array('alerts/notEnoughMoney'),301);
    ----------------------------------------------^^^^
    

    With this parameter bot will understand what this next page is permanent and will cache it as "main".

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号