Php Yii使用id和名称清除url
我希望我的url具有以下模式:Php Yii使用id和名称清除url,php,yii,seo,Php,Yii,Seo,我希望我的url具有以下模式: http://example.com/controller/view_id/title_name, like SO: http://stackoverflow.com/question/1/title Config.php: 'urlManager'=>array( 'urlFormat'=>'path', 'showScriptName'=>false, 'appendParams' =>
http://example.com/controller/view_id/title_name, like SO:
http://stackoverflow.com/question/1/title
Config.php:
'urlManager'=>array(
'urlFormat'=>'path',
'showScriptName'=>false,
'appendParams' => true,
'rules'=>array(
'user/<id:\d+>/<name:\w+>'=>array('user/view', 'caseSensitive'=>false),
'<controller>/<id:\d+>/<name:.*?>'=>'<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
),
)
我正在尝试将user/view/1
重定向到user/1/my_username
urluser/1/username
正在将我重定向到错误400无效请求
。
我缺少什么?如果您的用户和问题控制器有
操作视图
。这里的视图应该是操作的名称
试试这个:
'urlManager'=>array(
'urlFormat'=>'path',
'showScriptName'=>false,
'appendParams' => true,
'caseSensitive'=>false,//use case sensitivity here
'rules'=>array(
'<controller>/<view_id:\d+>/<title_name:[\w -\.]+>'=>'<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
),
)
在main.php中,这一行将读取您的url
'<controller>/<view_id:\d+>/<title_name:[\w -\.]+>'=>'<controller>/view',
//view_id and title_name you can change there name.
如果您使用的是url http://[yourhost]/user/1/username,则文件是必需的。另一方面,您可以通过http://[yourhost]/index.php/user/1/username进行检查。我已经更改了我的答案,您可以在那里进行检查。如果出现错误,请告诉我。提供控制器代码和您使用的url。为什么要在其中传递$id?你的URL是什么?试试这个公共函数actionView(){$this->render('view',array('model'=>$this->loadModel($\u GET['view\u id']),);}是的,但是我想知道如果有两条路径指向同一页面,搜索引擎优化是否有问题。
http://stackoverflow.com/question/1/title
'<controller>/<view_id:\d+>/<title_name:[\w -\.]+>'=>'<controller>/view',
//view_id and title_name you can change there name.
<controller>=question
<view_id:\d+>=1
<title_name:[\w -\.]+>=title
public function actionView()
{
$this->render('view', array( 'model' => $this->loadModel($_GET['view_id'])));
}