2015-08-13 16:56:50 5211次浏览 2条回答 4 悬赏 100 金钱

背景是这样的,最开始开发的时候没有做restful和版本控制
控制器在frontend/controllers/
模型frontend/models/

现在想新开发的功能开始用restful 和 版本控制
并且能将以前的也兼容到restful里
求一份配置文件


$params = array_merge(
    require(__DIR__ . '/../../common/config/params.php'),
    require(__DIR__ . '/../../common/config/params-local.php'),
    require(__DIR__ . '/params.php'),
    require(__DIR__ . '/params-local.php')
);

return [
    'id' => 'app-frontend',
    'basePath' => dirname(__DIR__),
    'bootstrap' => ['log'],
    #'controllerNamespace' => 'frontend\controllers',
    'controllerNamespace' => 'frontend\common\controllers',

    'modules' => [
        'v1' => [
            'basePath' => '@frontend/modules/v1',
        ],
        'v2' => [
            'basePath' => '@frontend/modules/v2',
        ],
    ],

    'components' => [

        'urlManager' => [
            'enablePrettyUrl' => true,
            'enableStrictParsing' => true,
            'showScriptName' => false,
            'rules' => [
                ['class' => 'yii\rest\UrlRule', 'controller' => ['user', 'user']],
                ['class' => 'yii\rest\UrlRule', 'controller' => ['v1/test', 'v1/post']],
                ['class' => 'yii\rest\UrlRule', 'controller' => ['v2/test', 'v2/post']],
            ],
        ],

        'user' => [
            'identityClass' => 'common\models\User',
            'enableAutoLogin' => true,
        ],
        'log' => [
            'traceLevel' => YII_DEBUG ? 3 : 0,
            'targets' => [
                [
                    'class' => 'yii\log\FileTarget',
                    'levels' => ['error', 'warning'],
                ],
            ],
        ],
        'errorHandler' => [
            'errorAction' => 'site/error',
        ],
    ],
    'params' => $params,
];


最佳答案

  • 性感的农民 发布于 2015-08-13 17:01 举报

    我是克隆了frontend对应的配置,然后将main.php改成这样子的:

    <?php
    $params = require(__DIR__ . '/params.php');
    return [
        // vendor目录
        'vendorPath' => dirname(dirname(__DIR__)) . '/vendor',
        // 目标语言
        'language' => 'en-US', #'zh-CN',
        // 组件
        'components' => [
            'cache' => [
                'class' => 'yii\caching\FileCache',
            ],
            // 配置RESTFul的urlManager
            'urlManager' => [
                'enablePrettyUrl' => true, // 支持伪静态
                'enableStrictParsing' => true, // <== restful想要这个
                'showScriptName' => false, // 不显示php文件名
                'rules' => [
                    [
                        'class' => 'yii\rest\UrlRule', 
                        'controller' => ['v1/default'],
                        'pluralize'=>false,
                        'extraPatterns' => [
    			'GET getToken' => 'get-token',
    			'POST getAdSequenceList' => 'get-ad-sequence-list',
                            'POST getAdTimerList' => 'get-ad-timer-list',
                            'POST getAdItem' => 'get-ad-item',
                            'GET search' => 'search',
                            'GET hello' => 'hello',
                            'GET list-user-less-than-age' => 'list-user-less-than-age',
                            'GET list' => 'list-user-less-than-age',
                        ],                
                    ],
                    //'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
                ],
            ],
            //国际化
            'i18n' => [
                'translations' => [
                    'app*' => [
                        'class' => 'yii\i18n\PhpMessageSource',
                        'basePath' => '@app/messages',
                        'sourceLanguage' => 'en',       //源语言使用en, 方便使用en-US为目标语言
                        'fileMap' => [
                            'app' => 'app.php',
                            'app/error' => 'error.php',
                        ],
                    ],
                ],
            ],
    
        ],
        // 模块
        'modules' => [
            'v1' => [
                'class' => 'app\modules\v1\Module',
            ],
        ],
        // app参数
        'params' => $params,
    ];
    
您需要登录后才可以回答。登录 | 立即注册
trylife
总监

trylife Beijing

注册时间:2015-03-26
最后登录:2020-12-07
在线时长:51小时55分
  • 粉丝39
  • 金钱1626
  • 威望40
  • 积分2536

热门问题