【问题标题】:yii2 advanced 2.06 createAbsoluteUrlyii2 高级 2.06 createAbsoluteUrl
【发布时间】:2015-12-16 21:24:27
【问题描述】:

我正在使用 urlManager 的 createAbsoluteUrl 方法来生成一个 URL,以通过消息 ID 查看我的消息模型的实例。

Yii::$app->urlManager->createAbsoluteUrl(['/message/view', 'id'=>$model->id, '#' => $model->id])

这会产生- http://localhost/advancedyii2/frontend/web/index.php?r=message#

但是,我需要—— http://localhost/advancedyii2/frontend/web/index.php?r=message%2Fview&id=1

你能找出问题所在吗?

谢谢, 噗

【问题讨论】:

  • 显示 config/main.php 组件 urlManager

标签: php yii2 yii2-advanced-app


【解决方案1】:

您需要为您的 url 编写规则: web.php 中的 URL 管理器规则配置

'components' => [
    'urlManager' => [               
        'showScriptName' => false,  // Disable index.php
        'enablePrettyUrl' => false, // Disable r= routes
        'enableStrictParsing' => true,
        'rules' => array(
                'mycategory/<controller:\w+>/<action:\w+>' => '<controller>/<action>',
                '<controller:\w+>/<id:\d+>' => '<controller>/view',
                '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
                '<controller:\w+>/<action:\w+>' => '<controller>/<action>',
                //Rules with Server Names
                'http://admin.domain.com/login' => 'admin/user/login',
                'http://www.domain.com/login' => 'site/login',
                'http://<country:\w+>.domain.com/profile' => 'user/view',
                '<controller:\w+>/<id:\d+>-<slug:[A-Za-z0-9 -_.]+>' => '<controller>/view',
            ),
    ],
],

您可以根据需要自定义您的网址。 More information

【讨论】:

    猜你喜欢
    • 2016-04-05
    • 1970-01-01
    • 2016-01-05
    • 2017-04-12
    • 2018-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多