在Yii框架中更好地使用什么URL操作


What URL manipulation is better to use in Yii framework

我在想在Yii框架中使用哪个解决方案更好

1( 从somethingController.php 重定向页面

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

||或

2( 正在创建Url

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

在使用cortroller&你需要的行动。

或者也许有更好的解决方案?

感谢

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

在某些情况下,当需要在没有用户操作的情况下更改页面时,您需要使用redirect,例如在控制器中:

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

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

  • 通过重定向避免不必要的步骤
  • 更适合SEO,对用户更友好
  • 更适合定制

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

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

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

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

有了这个参数,机器人将了解下一个页面是永久的,并将其缓存为"主"。

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

更好,因为它还可以与URL管理器一起工作,并提供重写URL。