我想要什么
我想将URL属性中的"+"替换为"-"。我正在使用Yii 2。
我想要使用"-"的URL。URL::to(…(生成带有"-"的URL。我希望用户在他的浏览器地址面板中看到"-">
示例:
这个
<siteneme>/hospital/U.S.A./Cleveland+Clinic
到此
<siteneme>/hospital/U.S.A./Cleveland-Clinic
我有什么
这是我的web.php
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'enableStrictParsing' => false,
'rules' => [
//Site controller, hospital action
'hospital/<location>/<name>' => 'site/hospital',
'<controller:'w+>/<action:'w+>' => '<controller>/<action>',
//removing 'controller' form URL
'<alias:index|search|detail|result|hospital>' => 'site/<alias>',
],
],
这就是在视图中生成URL的方式:
<?= Url::to([
'hospital',
'location' => $item->locations['name'],
'name' => $item->attributes['name'] ]); ?>
+
是由于对空格字符进行url编码而生成的。
如果你只想把+
改成-
,你可以这样做:
<?= Url::to([
'hospital',
'location' => str_replace(' ', '-', $item->locations['name']),
'name' => str_replace(' ', '-', $item->attributes['name'])
]); ?>
这将把名称中的每个空格都更改为-
(在这里的示例中,位置和名称都发生了更改(,并且不会修改url编码的-
。