Canonical Url

Hi,

I was using Url::canonical in my \layout\main.php view page to get canonical url.


$this->registerLinkTag(['rel' => 'canonical', 'href' => Url::canonical()]);

While checking it was found that Url::canonical() will give the canonical url with route and action parameters only if we declare the action parameters.


public function actionMyAction($param1, $param2){

//code

}



In the event of not declaring params and writing logic using if(isset($_GET['param1)) etc Url::canonical() returns a canonical url with only route and not with parameters.

Now, we have another property in the request which gives canonical url, which works perfect in all situations.


$this->registerLinkTag(['rel' => 'canonical', 'href' => Yii::$app->request->absoluteUrl]);

Is it better to use this instead of Url::canonical()?

As you’ve mentioned, Url::canonical() works based on params defined. If you don’t have your params defined, use registerLinkTag().