Yii app()->createUrl for view action

49,191

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.

Share:
49,191
JalalJaberi
Author by

JalalJaberi

As I see my technical aspect of life: He is a software engineer He Loves the technical dept. but he hates the technical debt He is a C++ evangelist He has a master degree (CS from Amirkabir university of Tehran) He has experienced broad type of IT projects, from simple web api's to real-time AR, computer vision, developing games, and so on He is actively searching for delighting jobs in some fields game industry subcategories in AI (CV, ML, ...) IOT

Updated on October 31, 2020

Comments

  • JalalJaberi
    JalalJaberi over 3 years

    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!