2016-05-11 65 views
1

我正在使用Yii2,我正在尝试更改Url 的空格破折号( - )如何替换破折号( - )而不是URL中的空格?

浏览器,我使用看跌期权加号(+)的代替空间的网址,在默认情况下;

比如这一句:

“如何更换仪表板,而不是空间”

变化:

/mysite.com/how+to+replace+dash+instead的+空格+

但我希望把破折号( - )代替加号(+)像打击网址:

/mysite.com/how-to-replace-dash-instead-of-space

+1

肯定这是因为 - (破折号)用于通过Yii2 URL路由惯例和匹配与动作名称大写.. – scaisEdge

+0

@scaisEdge我如何在yii2中检查它? – Farshid

+0

[Yii2 Url Html格式]可能的重复(http://stackoverflow.com/questions/26360249/yii2-url-html-format) –

回答

1

您可以使用htaccess的以下规则:

RewriteEngine on 

#1If uri has spaces, convert them to hyphens and set an env "hasspaces" 
RewriteRule (.*)\s(.*) $1-$2 [N,E=hasspaces:yes] 
#2if the env "hasspaces" is set, we will redirect spaces to hyphens 
RewriteCond %{ENV:hasspaces} yes 
RewriteRule ^(.+)$ /$1 [L,R] 

这将转换所有空格URI来连字符和重定向URL

  • example.com/hello字

  • example.com/hello-word
+0

RewriteEngine on RewriteCond%{REQUEST_FILENAME}!-f RewriteCond%{ REQUEST_FILENAME}!-d RewriteRule。 index.php – Farshid

+0

在我的htaccess中有代码,我如何将你的代码添加到该代码? – Farshid

+0

@法尔希德,你必须把这一点放在我发布的规则上,否则就会与此相冲突。 – starkeen

0

这样做一定是因为-(破折号)由Yii2 URL路由惯例和匹配使用,在行动上盒前命名

控制器类命名

Controller类的名字可以从 控制器ID根据以下过程得到:

转动的第一个字母在由连字符分隔成上壳体 每个单词。请注意,如果控制器ID包含斜杠,则此规则仅适用于ID中最后一个斜杠后面的部分 。删除连字符和 用反斜线替换任何正斜杠。追加后缀 控制器。预先安装控制器名称空间。以下是一些 示例,假设控制器名称空间采用默认值 app \ controllers:

article变成app \ controllers \ ArticleController; post-comment 成为app \ controllers \ PostCommentController; admin/post-comment 成为app \ controllers \ admin \ PostCommentController; adminPanels/post-comment变为 app \ controllers \ adminPanels \ PostCommentController。控制器类 必须是自动加载的。因此,在上面的例子中, 文章控制器类应该保存在别名为 @ app/controllers/ArticleController.php的文件中;而admin/post-comment 控制器应该位于 @ app/controllers/admin/PostCommentController.php中。

的完整指南请参阅本http://www.yiiframework.com/doc-2.0/guide-structure-controllers.htmlhttp://www.yiiframework.com/doc-2.0/guide-structure-controllers.html

相关问题