1. enablePrettyUrl
yii2默认不支持类似 http://<domain>/site/error 的url格式,需要在config.php中启用 enablePrettyUrl 属性
[ //others ‘components‘ => [ ‘urlManager‘ => [ ‘enablePrettyUrl‘ => true, ], ], ];
2. 配置suffix 实现伪静态 *.html
需要在 config.php中配置 urlManager 即可
[ ‘components‘ => [ ‘urlManager‘ => [ ‘enablePrettyUrl‘ => true, ‘suffix‘ => ‘.html‘, ], ], ];
3. 同时支持 http://<domain>/site/error.html 以及 http://<domain>/site/error 的url格式
* 没有找到仅仅配置config便可以实现的方式,这里重写UrlManager。只重写了一句代码,仅贴部分展示代码
namespace common\yiiext\web; use yii; use yii\web\UrlManager as BaseUrlManager; class UrlManager extends BaseUrlManager { public function parseRequest($request) { if ($this->enablePrettyUrl) { //other code ... if ($suffix !== ‘‘ && $pathInfo !== ‘‘) { $n = strlen($this->suffix); if (substr_compare($pathInfo, $this->suffix, -$n, $n) === 0) { $pathInfo = substr($pathInfo, 0, -$n); if ($pathInfo === ‘‘) { // suffix alone is not allowed return false; } } else { // 就这一句区别用父类 // suffix doesn‘t match return [$pathInfo, []]; } } //other code .. } }
* 然后再次配置config.php
[ ‘components‘ => [ ‘urlManager‘ => [ ‘class‘ => ‘common\yiiext\web\UrlManager‘, ‘enablePrettyUrl‘ => true, ‘suffix‘ => ‘.html‘, ], ], ];
时间: 2024-11-07 03:34:28