有没有办法在URL Yii2中将“+”转换为“ - ”?

I am trying to change the paramlinks to include the name of a post in my yii2 application.

example.com/item/hello+world

to

example.com/item/hello-world

These are the rules of in my urlmanager in frontend/config/main.php

'urlManager' => [
            'class' => 'yii\web\UrlManager',
            'enablePrettyUrl' => true,
            'showScriptName' => false,
            'enableStrictParsing' => false,
            'rules' => [
                '/' => 'site/index',
                'item/<title:[A-Za-z0-9 -_.]+>' => 'item/view',
            ],
        ]
$hi = 'example.com/item/hello+world';
$hi = str_replace('+', '-', $hi);

echo $hi;

like this you can replace what you need replacing with str_replace

output:

example.com/item/hello-world

The output url you displayed is put in a variable, then i fill in what I want replaced +, what should overwrite it is the - sign and then store it again in a variable called $hi again.

Your problem is the Url::toRoute method. By default, it will replace empty spaces by a "+". And there is no configuration to change that (at least I did not find).

You could use a str_replace like @baboizk mentioned, or, if you want to cover any accents, symbols, etc. You can use BaseInflector::slug. Example:

Url::toRoute(['item/view', 'title' => BaseInflector::slug($model->title)]);

But i'm still not sure how your actionItem works, because it probably needs to search the model by it's title and you are changing it.