简体   繁体   English

在Yii的SEO友好网址

[英]SEO friendly url in Yii

I need a rule for http://example.com/post/view/id/1 url that will be displayed like this http://example.com/post/post_title . 我需要http://example.com/post/view/id/1网址的规则,该规则将显示为http://example.com/post/post_title Instead of the id number i want to display the post name or title. 而不是身份证号码我想显示帖子名称或标题。

My config looks 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>',
        ),
                    'showScriptName'=>false,
    ),

you need to have a rule that looks like this 你需要一个看起来像这样的规则

'post/<post_title:\w+>'=>'postController/view/title/<post_title>',

This just tells the app to forward requests that start with a post/post_title to the post controller's actionView where $_GET['title'] is set to post_title... 这只是告诉应用程序将以post / post_title开头的请求转发到post控制器的actionView,其中$ _GET ['title']设置为post_title ...

Hope that helps. 希望有所帮助。

It's simple rule 这是一个简单的规则

'post/<post_title:\w+>' => 'post/view'

If need url like this http://example.com/post/some-title-with-hyphen 如果需要这样的网址http://example.com/post/some-title-with-hyphen

'post/<post_title:[-\w]+>' => 'post/view'

And you need a controller action with $post_title argument 而且你需要一个带有$ post_title参数的控制器动作

public function actionView($post_title) {
    ...

in default, Yii only retreive the post id, if you want to display the post title, you need to access the DB your self in a custom Url class rules. 默认情况下,Yii只会检索帖子ID,如果要显示帖子标题,则需要在自定义Url类规则中访问自己的DB。 there is an example in the yii tutorial called using-custom-url-rule-classes 在yii教程中有一个名为using-custom-url-rule-classes的示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM