Php 如何更改yii中的url管理流程

Php 如何更改yii中的url管理流程,php,yii,bootstrapping,Php,Yii,Bootstrapping,我已经在yii实施了该项目。我完成了我的项目,但我想更改id而不是名称。ie url管理。我在config.php中取消了注释,然后添加了以下代码。这些建议如下: 我的表名是recipe: public function loadModel($id) { $model=Recipe::model()->findByPk($id); if($model===null) throw new CHttpException(404,'The requested pa

我已经在yii实施了该项目。我完成了我的项目,但我想更改id而不是名称。ie url管理。我在config.php中取消了注释,然后添加了以下代码。这些建议如下: 我的表名是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;
}
除此之外,我添加了sitecontroller使用配方的顶部。但这表明错误是存在的

具有非复合名称“Recipe”的use语句无效
请建议我合适的答案

我个人在我的项目中使用类似于/product name/p/1的东西,这对搜索引擎优化很友好。要使你的链接看起来像这样,你必须首先更改你的url规则

    'urlManager' => array(
        'urlFormat' => 'path',
        'showScriptName' => false,
        'urlSuffix' => '/',
        'rules' => array(
            '<title:.*?>/p/<id:\d+>'=>'product/view',
        ),
    ),
现在它可以双向工作,createURL将始终创建像/product name/p/1这样的url,而且您还可以以正常方式显示产品

/**
 * 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,
    ));
}

我不明白你的标签。。例如,我的表名是recipe字段是recipe\u id、name等。我想显示recipe\u id而不是name。如何通过你的代码帮助我
/**
 * 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,
    ));
}