简体   繁体   中英

Yii app()->createUrl for view action

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!

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 .

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.

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? :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM