This post will be a reference on how deal with urls, links and
browser redirects in Yii Framework. It will guide you on how to use
different built-in functions in Yii like createUrl(),
createAbsoluteUrl(), CHtml::link(), redirect().
Create URL
$url
= Yii::app()->createUrl(
'site/index'
);
Result: index.php?r=site/index
You will obtain this format of URL if you're not using the URL Manager to make your links more user-friendly.
|
$url = Yii::app()->createUrl( 'site/index' , array ( 'id' =>100));
|
Result: index.php?r=site/index&id=100
Create URL from Controller
|
Yii::app()->controller->createUrl( "index" , array ( "id" =>100));
|
Result: index.php?r=site/index&id=100
You will obtain this result if your current controller is "SiteController".
If you are in a controller you can simply write
|
$this ->createUrl( 'index' , array ( 'id' =>100));
|
Create absolute URL:
In order to create an absolute path url you need to use createAbsoluteUrl() function:
|
Yii::app()->createAbsoluteUrl( 'site/index' , array ( 'id' =>100));
|
Result: http:⁄⁄yourdomain.tld⁄index.php?r=site⁄index&id=100
Create Link
You can use the CHTML helper to quickly create an anchor in HTML:
|
echo CHtml::link( 'text' , array ( 'site/index' , 'id' =>100));
|
Result: <a href="/ABC/index.php?r=site/index&id=100″>text</a>
Redirect Browser
|
$this ->redirect( array ( 'site/index' , 'id' =>100));
|
It will redirect the browser to index.php?r=site/index&id=100
No comments:
Post a Comment