set base url in createAbsoluteUrl

Hi guys,

my yii mobile application generate url starts with http://m.myexample.com/… and my mobile website is accessed at http://m.myexample.com

I want createAbsoluteUrl return url starting with http://myexample.com/… which is mainly used in sending out email so that all links in email pointing to desktop url (http://myexample.com)

IS there any configs I can do ?

I’ve done the following in order to ensure that the correct domain is used in canonical tags. This is very similar to what you’re trying to achieve, but it’s only tested for my URL manager configuration:

protected/config/main.php




	'params' => array(

		'canonicalDomain' => 'www.example.com',

	),




protected/components/Controller.php




	private $canonicalUrl;

	

	/**

	 * Prevent the display of a canonical URL, for instance on error pages.

	 */

	public function noCanonicalUrl()

	{

		$this->canonicalUrl = '';

	}


	/**

	 * Sets the canonical URL for the current page using the specified route and parameters.

	 * 

	 * @param string the route in the form '/controllerId/actionId'.

	 * @param array() additional GET parameters.

	 */

	public function setCanonicalUrl($route, $params = array())

	{

		$this->canonicalUrl = 'http://'

			. Yii::app()->params['canonicalDomain']

			. $this->createUrl($route, $params);

	}


	/**

	 * Gets the canonical URL for the current page. If this has not been explicitly set, it

	 * will be generated from the current controller and action IDs. It uses the http

	 * protocol and the canonicalDomain configuration parameter.

	 * 

	 * @return string the canonical URL.

	 */

	public function getCanonicalUrl()

	{

		if ($this->canonicalUrl !== null)

			return $this->canonicalUrl;


		return 'http://'

			. Yii::app()->params['canonicalDomain']

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

	}



The part that you’re interested in is:




return 'http://'

	. Yii::app()->params['canonicalDomain']

	. $this->createUrl($route, $params);



Feel free to use something similar to that.

Hi Keith,

are you suggesting that instead of doing


Yii::app()->createAbsoluteUrl($route, $params)

I should do


'http://'.Yii::app()->params['canonicalDomain'].$this->createUrl($route, $params);

I try to use your components/Controller.php code but it doesnt work so I end up with the above assumption.

Thanks

Basically, yes. I just refactored it out into a method.