Yii2:yii \ web \ UrlManager确实没有用户友好的网址

时间:2018-05-27 20:07:09

标签: php yii2

这里的urlManager配置:

$stmt->bindParam(':subtotal', $subTotal);

这里的urlManager使用:

return [
    'class'               => \yii\web\UrlManager::class,
    'enablePrettyUrl'     => true,
    'showScriptName'      => false,
    'baseUrl'             => '',
    'rules'               => [
        'page/<id:[\\w-_]+>' => 'page/index',
    ],
];

以下是浏览器的结果:

$menuItems = [ [ 'label' => 'Home', 'url' => Yii::$app->urlManager->createAbsoluteUrl(['/page']), ], [ 'label' => 'About', 'url' => Yii::$app->urlManager->createAbsoluteUrl(['/page', 'id' => 'about']), ], [ 'label' => 'Contact', 'url' => Yii::$app->urlManager->createAbsoluteUrl(['/page', 'id' => 'contact']), ], ];

http://localhost/page?id=about

我的代码有什么问题?

1 个答案:

答案 0 :(得分:1)

创建网址时需要使用确切的路线:

$menuItems = [
    [
        'label' => 'Home',
        'url' => Yii::$app->urlManager->createAbsoluteUrl(['/page']),
    ],
    [
        'label' => 'About',
        'url' => Yii::$app->urlManager->createAbsoluteUrl(['/page/index', 'id' => 'about']),
    ],
    [
        'label' => 'Contact',
        'url' => Yii::$app->urlManager->createAbsoluteUrl(['/page/index', 'id' => 'contact']),
    ],
];