<legend id='fO6bi'><style id='fO6bi'><dir id='fO6bi'><q id='fO6bi'></q></dir></style></legend>

      <small id='fO6bi'></small><noframes id='fO6bi'>

      • <bdo id='fO6bi'></bdo><ul id='fO6bi'></ul>

    1. <i id='fO6bi'><tr id='fO6bi'><dt id='fO6bi'><q id='fO6bi'><span id='fO6bi'><b id='fO6bi'><form id='fO6bi'><ins id='fO6bi'></ins><ul id='fO6bi'></ul><sub id='fO6bi'></sub></form><legend id='fO6bi'></legend><bdo id='fO6bi'><pre id='fO6bi'><center id='fO6bi'></center></pre></bdo></b><th id='fO6bi'></th></span></q></dt></tr></i><div id='fO6bi'><tfoot id='fO6bi'></tfoot><dl id='fO6bi'><fieldset id='fO6bi'></fieldset></dl></div>
      <tfoot id='fO6bi'></tfoot>

      如何使用 Yii2 Sluggable Behavior?

      How to use Yii2 Sluggable Behavior?(如何使用 Yii2 Sluggable Behavior?)
      • <bdo id='vO3W3'></bdo><ul id='vO3W3'></ul>

                <tbody id='vO3W3'></tbody>

                <i id='vO3W3'><tr id='vO3W3'><dt id='vO3W3'><q id='vO3W3'><span id='vO3W3'><b id='vO3W3'><form id='vO3W3'><ins id='vO3W3'></ins><ul id='vO3W3'></ul><sub id='vO3W3'></sub></form><legend id='vO3W3'></legend><bdo id='vO3W3'><pre id='vO3W3'><center id='vO3W3'></center></pre></bdo></b><th id='vO3W3'></th></span></q></dt></tr></i><div id='vO3W3'><tfoot id='vO3W3'></tfoot><dl id='vO3W3'><fieldset id='vO3W3'></fieldset></dl></div>
                <tfoot id='vO3W3'></tfoot>

              1. <legend id='vO3W3'><style id='vO3W3'><dir id='vO3W3'><q id='vO3W3'></q></dir></style></legend>

                <small id='vO3W3'></small><noframes id='vO3W3'>

                本文介绍了如何使用 Yii2 Sluggable Behavior?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我已按照文档说明定义了此行为.

                I have defined this behavior as per documentation instructions.

                public function behaviors()
                {
                    return [
                        TimestampBehavior::className(),
                        [
                            'class' => SluggableBehavior::className(),
                            'attribute' => 'title',
                        ],
                    ];
                }
                

                在我的配置 url 管理器中,我定义了这样的自定义规则:example.com/article/1

                In my config url manager I have defined custom rule like this: example.com/article/1

                'urlManager' => [
                    'class' => 'yiiwebUrlManager',
                    'enablePrettyUrl' => true,
                    'showScriptName' => false,
                    'rules' => [
                        'article/<id:d+>/<slug>' => 'article/view',
                    ],
                ],
                

                我的查看操作是:

                public function actionView($id, $slug = null)
                {
                    return $this->render('view', [
                        'model' => $this->findModel($id),
                    ]);
                }
                

                在我的索引视图文件中,我生成 URL 来查看这样的操作: Url::to(['article/view', 'id' => $model->id, 'slug' => $model->slug])

                In my index view file I am generating URL to view action like this : Url::to(['article/view', 'id' => $model->id, 'slug' => $model->slug])

                我想像这样在 url 中输出我的文章标题:example.com/article/1/My-first-post

                I would like to output my article title in url like this: example.com/article/1/My-first-post

                但我没有在 URL 中获得标题.

                But I am not getting title in URL.

                Soju 说 slug 是一个数据库属性.我在我的文章表中创建了一个名为 slug 的新列,它是 varchar 1024.但我仍然没有在 URL 中生成 slug.我的网址是:example.com/article/1

                Soju said that slug is a database attribute. I have created new column in my article table called slug and it is varchar 1024. But I am still not getting slug generated in URL. My URL is: example.com/article/1

                怎么了?谢谢

                编辑:我已经更新了我的代码以将标题值插入到我的文章表的 slug 列中.现在我开始工作了,但我没有得到 SEO URL-s.我明白了:article/1/First+Article,我想要article/1/First-Article.

                EDIT: I have updated my code to insert title value into slug column in my article table. Now I get slug working but I do not get SEO URL-s. I get this: article/1/First+Article, and I would like article/1/First-Article.

                我尝试过:

                return [
                    TimestampBehavior::className(),
                    [
                        'class' => SluggableBehavior::className(),
                        'attribute' => 'title',
                        'value' => function ($event) {
                            return str_replace(' ', '-', $this->slug);
                        }
                    ],
                ];
                

                这也不起作用:return str_replace(' ', '-', $this->slug);

                推荐答案

                您可以添加以下 urlManager 规则:

                You could add the following urlManager rule :

                'article/<id:d+>/<slug>' => 'article/view',
                

                并在您的视图中构建 url,如下所示:

                And build url in your views like this :

                yiihelpersUrl::to(['article/view', 'id'=>$model->id, 'slug'=>$model->slug])
                

                您还可以在模型中添加助手:

                You could also add helpers in your model :

                public function getRoute()
                {
                    return ['article/view', 'id'=>$this->id, 'slug'=>$this->slug];
                }
                
                public function getUrl()
                {
                    return yiihelpersUrl::to($this->getRoute());
                }
                

                然后在您的视图中简单地使用 $model->url.

                And then simply use $model->url in your views.

                这篇关于如何使用 Yii2 Sluggable Behavior?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Is Joomla 2.5 much faster than Joomla 1.5 Querywise(Joomla 2.5 比 Joomla 1.5 Querywise 快得多吗)
                How to share Joomla login session from one joomla website to one ASP.Net MVC website(如何将 Joomla 登录会话从一个 joomla 网站共享到一个 ASP.Net MVC 网站)
                htaccess redirect root to subdirectory but allow index.php in root AND query strings to function(htaccess 将根重定向到子目录,但允许根和查询字符串中的 index.php 起作用)
                Joomla include database functions(Joomla 包含数据库功能)
                nl2br() not working when displaying SQL results(显示 SQL 结果时 nl2br() 不起作用)
                Joomla 2.5 JFactory::getSession(); seems to be caching in firefox(Joomla 2.5 JFactory::getSession();似乎在 Firefox 中缓存)
                <legend id='hSkhr'><style id='hSkhr'><dir id='hSkhr'><q id='hSkhr'></q></dir></style></legend>
                <i id='hSkhr'><tr id='hSkhr'><dt id='hSkhr'><q id='hSkhr'><span id='hSkhr'><b id='hSkhr'><form id='hSkhr'><ins id='hSkhr'></ins><ul id='hSkhr'></ul><sub id='hSkhr'></sub></form><legend id='hSkhr'></legend><bdo id='hSkhr'><pre id='hSkhr'><center id='hSkhr'></center></pre></bdo></b><th id='hSkhr'></th></span></q></dt></tr></i><div id='hSkhr'><tfoot id='hSkhr'></tfoot><dl id='hSkhr'><fieldset id='hSkhr'></fieldset></dl></div>
                  <bdo id='hSkhr'></bdo><ul id='hSkhr'></ul>
                    <tbody id='hSkhr'></tbody>

                  <small id='hSkhr'></small><noframes id='hSkhr'>

                    • <tfoot id='hSkhr'></tfoot>