Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在Yii框架中使用什么URL操作更好_Php_Url_Redirect_Yii - Fatal编程技术网

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

Php 在Yii框架中使用什么URL操作更好,php,url,redirect,yii,Php,Url,Redirect,Yii,我想知道在Yii框架中使用哪种解决方案更好 1) 从somethingController.php重定向页面 $this->redirect(array($this->id."/something")); ||或 2) 创建Url $this->createUrl($this->id."/something"); 在视图中使用控制器&您需要的操作 或者有更好的解决办法 谢谢 $this->createUrl($this->id."/".$this->

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

1) 从somethingController.php重定向页面

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

2) 创建Url

$this->createUrl($this->id."/something");
在视图中使用控制器&您需要的操作

或者有更好的解决办法

谢谢

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

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

这就像问
什么是更好的英里或磅?
。功能是非常不同的

在某些情况下,如在控制器中,需要在无需用户操作的情况下更改页面时,需要使用
重定向

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>

使用此参数,机器人将了解下一页的永久内容,并将其缓存为“main”。

我不理解您的问题。你举的两个例子是不同的。一个重定向到另一个操作,另一个创建URL。对不起,我的错。我改了。非常感谢,我知道我的问题是“英里和磅”,但这对我来说是全新的,我正在努力理解它,再次感谢,你帮助了我:)
$this->redirect(array('alerts/notEnoughMoney'),301);
----------------------------------------------^^^^