在yii中,有时候会调用CHtml的normalizeUrl去生成一个url。调用它的好处是当配置文件中的urlManager中url的映射的rules发生变化时,它也会自动发生变化。但如果在console中你调用同样的方法,得到的却是类似“项目protect绝对路径/xxxxx/xxx.html”,而不是你希望的http:/www.yyyy.com/xxxx/xxx.html这样的结果。原因很简单因为调用console是本地调用,而不是一个http请求,自然就没有host信息。所以它只能返回给你一个路径。
我的解决方法是在components/Controller.php中修改
class Controller extends CController
{
public $layout='//layouts/column1';
public $menu=array();
public $breadcrumbs=array();
protected $_gets;
protected $_baseUrl;
protected $_xsession;
/**
* 初始化
* @see CController::init()
*/
public function init ()
{
Yii::app()->getRequest()->setBaseUrl("http://www.58xx.com");//在这里设置带域名的URL
$this->_xsession = new CHttpSession();
$this->_xsession->open();
$this->_gets = Yii::app()->request;
$this->_baseUrl = Yii::app()->baseUrl;
}
}
上面的代码最核心的就是这句
Yii::app()->getRequest()->setBaseUrl("http://www.58xx.com");//在这里设置带域名的URL
设置这个后,因为所有的Controller都是继承Controller.php这个类的 当你在view中用createUrl时就会就是自动添加上域名的(不会像以前那样得到的是一个相对的路径, 全路径在SEO中效果要好于相当路径的 )!