Yii2 在 URL 中更改 id
Yii2 Change id in URL
是否可以将 Yii2 basic 中的 URL 中的 id 更改为其他内容我的 URL 实际是
http://localhost:8585/yii40/wfp/web/post/view?id=368
我想改成
http://localhost:8585/yii40/wfp/web/post/view?post=368
我的观点是
public function actionView($id)
{
return $this->render('view', [
'model' => $this->findModel($id),
]);
}
看来您只想更改GET
参数的名称(id->post)。
假设您有默认的应用程序配置,您需要在适当的控制器 (PostController.php) 中找到视图方法(称为 actionView)。
该方法要么将 $id 参数作为其参数(如 public function actionView($id)
),要么稍后从 $_GET 超全局数组中检索 'id'(如 $modelId = $_GET['id'];
或 $modelId = Yii::$app->request->get('id');
)
这是你改的地方
要更好地了解 Yii2 应用程序结构和处理请求的方式,请参阅 http://www.yiiframework.com/doc-2.0/guide-index.html#application-structure
它与 link 相关,点击该动作时它可能是
在您的 GridView
、/your_project_root/views/post/index.php
文件中,您点击此处查看 post 详细信息,方法是提交id.
或者在你看来某处的正常link
1) 对于 GridView,转到您的操作列并将 ['class' => 'yii\grid\ActionColumn']
更改为以下
[ 'class' => 'yii\grid\ActionColumn' ,
'header' => 'Actions' ,
'template'=>'{view}{update}{delete}',
'buttons'=>[
'view'=>function($url,$model){
$html = '<span class="glyphicon glyphicon-search"></span>';
return Html::a($html,["post/view",'post'=>$model->id]);
}
],
] ,
并将 PostController
中的 actionView($id)
更改为 actionView($post)
,并在操作代码中将所有出现的 $id
替换为 $post
。
2) 如果它是正常的 link 那么你必须像下面那样为 link 更改 url
Html::a($html,["post/view",'post'=>$model->id]);
其中 $model
应替换为适当的变量。
是否可以将 Yii2 basic 中的 URL 中的 id 更改为其他内容我的 URL 实际是
http://localhost:8585/yii40/wfp/web/post/view?id=368
我想改成
http://localhost:8585/yii40/wfp/web/post/view?post=368
我的观点是
public function actionView($id)
{
return $this->render('view', [
'model' => $this->findModel($id),
]);
}
看来您只想更改GET
参数的名称(id->post)。
假设您有默认的应用程序配置,您需要在适当的控制器 (PostController.php) 中找到视图方法(称为 actionView)。
该方法要么将 $id 参数作为其参数(如 public function actionView($id)
),要么稍后从 $_GET 超全局数组中检索 'id'(如 $modelId = $_GET['id'];
或 $modelId = Yii::$app->request->get('id');
)
这是你改的地方
要更好地了解 Yii2 应用程序结构和处理请求的方式,请参阅 http://www.yiiframework.com/doc-2.0/guide-index.html#application-structure
它与 link 相关,点击该动作时它可能是
在您的
GridView
、/your_project_root/views/post/index.php
文件中,您点击此处查看 post 详细信息,方法是提交id.或者在你看来某处的正常link
1) 对于 GridView,转到您的操作列并将 ['class' => 'yii\grid\ActionColumn']
更改为以下
[ 'class' => 'yii\grid\ActionColumn' ,
'header' => 'Actions' ,
'template'=>'{view}{update}{delete}',
'buttons'=>[
'view'=>function($url,$model){
$html = '<span class="glyphicon glyphicon-search"></span>';
return Html::a($html,["post/view",'post'=>$model->id]);
}
],
] ,
并将 PostController
中的 actionView($id)
更改为 actionView($post)
,并在操作代码中将所有出现的 $id
替换为 $post
。
2) 如果它是正常的 link 那么你必须像下面那样为 link 更改 url
Html::a($html,["post/view",'post'=>$model->id]);
其中 $model
应替换为适当的变量。