Yii2 之 UrlManager 实践 (一)
2021-06-24 01:05
标签:info div extend $path 属性 tty manage 重写 config 1. enablePrettyUrl yii2默认不支持类似 http:// 2. 配置suffix 实现伪静态 *.html 需要在 config.php中配置 urlManager 即可 3. 同时支持 http:// * 没有找到仅仅配置config便可以实现的方式,这里重写UrlManager。只重写了一句代码,仅贴部分展示代码 * 然后再次配置config.php Yii2 之 UrlManager 实践 (一) 标签:info div extend $path 属性 tty manage 重写 config 原文地址:http://www.cnblogs.com/gouge/p/7159712.html[
//others
‘components‘ => [
‘urlManager‘ => [
‘enablePrettyUrl‘ => true,
],
],
];
[
‘components‘ => [
‘urlManager‘ => [
‘enablePrettyUrl‘ => true,
‘suffix‘ => ‘.html‘,
],
],
];
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 ..
}
}
[
‘components‘ => [
‘urlManager‘ => [
‘class‘ => ‘common\yiiext\web\UrlManager‘,
‘enablePrettyUrl‘ => true,
‘suffix‘ => ‘.html‘,
],
],
];