Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Yii app()->createUrl for view action

Tags:

url

php

yii

I use generated actions by Gii in a module said News. I have normal view action that works with an id parameter (such as example.com/news/view/id/1).
When I use this line of code:

Yii::app()->createUrl("news/view",array("id"=>$data->primaryKey))

It generates example.com/news/1 (if $data->primaryKey is 1). It is not correct.
When I use this line of code:

Yii::app()->createUrl("news/view/id/",array("id"=>$data->primaryKey))

It generates example.com/news/id/id/1 (if $data->primaryKey is 1).

I am so confused! in first situation, this function doesn't generate id as a parameter name, and in second situation, it does! but after manually added id.
What shoud I do to make correct url format with this function?

Edit: news is a module. I changed the line of code as:

Yii::app()->createUrl("news/default/view/id/",array("id"=>$data->primaryKey))

It generates example.com/news/default/view/id/1 that is correct, but I don't want that default!

like image 397
JalalJaberi Avatar asked Dec 21 '25 12:12

JalalJaberi


1 Answers

In config file you have something like this:

'urlManager'=>array(
            'urlFormat'=>'path',
            'rules'=>array(
                '<controller:\w+>/<id:\d+>'=>'<controller>/view',
                '<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
                '<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
            ),
        ),

this create how to look the URL.

You not need to write the id parameter when you create URL because is default. Look on the urlmanager rules:

Yii::app()->createUrl("news/view/",array("id"=>$data->primaryKey)) => example.com/news/id/1

On module defaut:

Yii::app()->createUrl('/news/default/view', array('id' => $data->primaryKey))

You need to create the urlmanager rule... how you want to look at your URL. More details here.

like image 108
TotPeRo Avatar answered Dec 23 '25 01:12

TotPeRo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!