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

时间:2013-04-30 06:42:57

标签: php url redirect yii

我徘徊哪种解决方案更适合在Yii框架中使用,

1)从somethingController.php重定向页面

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

||或

2)创建网址

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

使用contoroller& amp;你需要的行动。

或许有更好的解决方案?

谢谢

2 个答案:

答案 0 :(得分:1)

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

更好,因为它也适用于URL管理器并提供重写URL。

答案 1 :(得分:1)

就像问what is better miles or pounds?一样。这些功能非常不同。

在某些情况下,如果需要更改页面而无需用户操作,则需要使用redirect,例如控制器

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

如果您想生成 html链接中使用的地址,那么您需要使用createUrl,因为它会:

  • 避免重定向的不必要步骤
  • 更适合搜索引擎优化,并且更加用户友好
  • 更适合自定义

您可以在视图中使用createUrl,例如:

<?php
$link = $this->createUrl(array('user/profile'));
?>

<a href="<?php echo $link ?>">My Profile</a>

在任何情况下,如果您使用重定向搜索机器人可见的内容,则需要添加第二个参数:

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

使用此参数,bot将了解下一页是永久性的,并将其缓存为“main”。