3

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!

JalalJaberi
  • 2,417
  • 8
  • 25
  • 41

3 Answers3

4

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.

Mukyuu
  • 6,436
  • 8
  • 40
  • 59
TotPeRo
  • 6,561
  • 4
  • 47
  • 60
0

Use

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

if you have a module names news.

You can try to replace news with regex but you will face problems with urls matched by several regexes if your regex is too broad. Use something like <module:news|accounting|people> in rules array key and <module> in rules array value.

If you need more sophisticated url management, or if you cannot solve your task with regexes, you can always extend CUrlManager.

George Sovetov
  • 4,942
  • 5
  • 36
  • 57
0

trying to check it on this directory: protected/config/main.php sir.


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

Please notice is that routed well? :)

TotPeRo
  • 6,561
  • 4
  • 47
  • 60