【问题标题】:rest yii2 pretty url urlManagerrest yii2 漂亮的 url urlManager
【发布时间】:2016-10-09 11:01:05
【问题描述】:

我的 yii2 rest 可以很好地处理这个请求 http://extractor-frontend.dev/property?id=JP000004 我会和这个一起工作 http://extractor-frontend.dev/property/JP000004

这是我在 config/web.php 中的 urlManager

urlManager' => [
        'enablePrettyUrl' => true,
        'enableStrictParsing' => true,
        'showScriptName' => false,

        'rules' => [
            [
                'class'=>'yii\rest\UrlRule',
                'pluralize' => false,
                'controller' => 'property',

                'tokens' => [
                    '{id}' => '<id:\\w+>'
                ],
                'extraPatterns' => ['GET,HEAD property/{id}' => 'index',]

            ]
        ],
    ],

这是我在网络上的 .htaccess

RewriteEngine on
Options Indexes
Require all granted
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Otherwise forward it to index.php
RewriteRule . index.php

如果把'enableStrictParsing' => false,

http://extractor-frontend.dev/site/about 工作正常...重写规则有效!

【问题讨论】:

    标签: yii2 yii2-advanced-app yii-rest


    【解决方案1】:

    我将以下代码用于我的 yii2 应用程序。我认为您的配置方法符合 yii1 的条件。推荐使用yii2 config方式。

     'urlManager' => [
            'enablePrettyUrl' => true,
            'showScriptName' => false,
            'rules' => [
                'myaction/<id>/<param2>' => 'site/myaction',
    
                [
                    'pattern' => '<action>',
                    'route' => 'site/<action>',
                    'defaults' => ['action' => 'index']
                ],
            ]
        ]
    

    【讨论】:

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