在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

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".

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

is the better because it will work with URL manager also and give rewrite urls.