Question

i have implementing the project in yii. i done my project but i want to change instead of id to name. ie url management. i did uncomment in config.php then i added the following code. those follows: my table name is recipe:

public function loadModel($id)
{
    $model=Recipe::model()->findByPk($id);
    if($model===null)
        throw new CHttpException(404,'The requested page does not exist.');
    return $model;
}

     public function loadModel2($name)
{
            $model=Recipe::model()->find('t.name=:name', array(':name' => $name));
    if($model===null)
        throw new CHttpException(404,'The requested page does not exist.');
    return $model;
}

except this i added top of the sitecontroller use Recipe. but it shows error is

The use statement with non-compound name 'Recipe' has no effect please suggest me suitable answer

Pas de solution correcte

Autres conseils

I personally use in my projects something like /product-name/p/1 and this is SEO friendly. To get your links to look like that you have to first change your url rules

    'urlManager' => array(
        'urlFormat' => 'path',
        'showScriptName' => false,
        'urlSuffix' => '/',
        'rules' => array(
            '<title:.*?>/p/<id:\d+>'=>'product/view',
        ),
    ),

Then use this to create your URLs.

Yii::app()->createUrl('product/view',array('id'=>$model->id, 'title'=>$model->name))

Now it works both ways, the create url will always create urls like /product-name/p/1 and further more you can show the product the normal way

/**
 * Displays a particular model.
 * @param integer $id the ID of the model to be displayed
 */
public function actionView($id)
{
    $model = $this->loadModel($id);
    $this->render('view',array(
        'model'=>$model,
    ));
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top