如何在web.php的urlManager中实现patterns数组?

问题描述 投票:0回答:1

我有一个 Yii2 应用程序。我尝试在 web.php 的 urlManager 中添加模式数组。我进行了很多搜索并找到了很多具有正确解决方案的链接。例如:

https://stackoverflow.com/questions/41873686/yii2-apply-custom-url-rule-class
https://www.yiiframework.com/doc/guide/2.0/en/runtime-routing

但这并没有解决问题。

所以如果我这样做:

'urlManager' => [
            'enablePrettyUrl' => true,
            'showScriptName' => false,
            
            'rules' =>  [
                'class' => 'yii\rest\UrlRule', 
                'controller' => 'v1/user', 
                'POST' => 'v1/user/signup',
                'POST' => 'v1/user/login',
                
                
            ],                          
            
        ],

在邮递员中,我在这个网址上发了一篇文章:

http://localhost:8080/v1/user/login

一切正常。但如果我这样做:

        'urlManager' => [
            'enablePrettyUrl' => true,
            'showScriptName' => false,
            
            'rules' =>  [
                'class' => 'yii\rest\UrlRule', 
                'controller' => 'v1/user', 
                'patterns' => [
                    'POST' => 'signup',
                    'POST login' => 'login',
                    'OPTIONS login' => 'options',                    
                ],
                
                
            ],                         
            
        ],

整个 web.php 文件看起来:

<?php
// phpcs:ignoreFile

$params = require __DIR__ . '/params.php';
$db = require __DIR__ . '/db.php';

$config = [
    'id' => 'basic',
    'name' => 'internetsuite 2.0',
    'basePath' => dirname(__DIR__),
    'bootstrap' => ['log'],
    'aliases' => [
        '@bower' => '@vendor/bower-asset',
        '@npm'   => '@vendor/npm-asset',
    ],
    'modules' => [
        'v1' => [
            'class' => 'app\modules\v1\Module',
        ],
    ],
    'components' => [
        'request' => [

            'parsers' => [
                'application/json' => 'yii\web\JsonParser',
            ],
            // !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
            'cookieValidationKey' => 'OEtCunrAfQNETtmUSDnZw1JPHTB44i3A',
        ],
        

        'cache' => [
            'class' => 'yii\caching\FileCache',
        ],
        'user' => [
            'identityClass' => 'app\models\User',
            'enableAutoLogin' => true,
        ],
        'errorHandler' => [
            'errorAction' => 'site/error',
        ],
        'mailer' => [
            'class' => \yii\symfonymailer\Mailer::class,
            'viewPath' => '@app/mail',
            // send all mails to a file by default.
            'useFileTransport' => true,
        ],
        'log' => [
            'traceLevel' => YII_DEBUG ? 3 : 0,
            'targets' => [
                [
                    'class' => 'yii\log\FileTarget',
                    'levels' => ['error', 'warning'],
                ],
            ],
        ],
        'db' => $db,
        'urlManager' => [
            'enablePrettyUrl' => true,
            'showScriptName' => false,
            'enableStrictParsing' => true,
            
            'rules' =>  [[
                'class' => 'yii\rest\UrlRule', 
                'controller' => 'v1/user', 
                'pluralize' => false,
                ],
                'extraPatterns' => [
                    'OPTIONS {login}' => 'options',                    
                    'POST signup' => 'signup',
                    'POST login' => 'login',
                ],
                
                
            ],                         
            
        ],

    ],
    'params' => $params,
];

if (YII_ENV_DEV) {
    // configuration adjustments for 'dev' environment
    $config['bootstrap'][] = 'debug';
    $config['modules']['debug'] = [
        'class' => 'yii\debug\Module',
        // uncomment the following to add your IP if you are not connecting from localhost.
        //'allowedIPs' => ['127.0.0.1', '::1'],
    ];

    $config['bootstrap'][] = 'gii';
    $config['modules']['gii'] = [
        'class' => 'yii\gii\Module',
        // uncomment the following to add your IP if you are not connecting from localhost.
        //'allowedIPs' => ['127.0.0.1', '::1'],
    ];
}

return $config;

我收到此错误:

Unknown Property – yii\base\UnknownPropertyException
Setting unknown property: yii\web\UrlRule::POST

我需要 OPTIONS 参数,因为我从 React 应用程序调用 api 调用。

问题:如何正确实现urlManager中的模式?

php yii2 yii2-urlmanager
1个回答
0
投票

文档进行比较时,这些规则显得有点荒谬。

可能更相似:

'patterns' => [
    'POST v1/user'       => 'signup',
    'POST v1/user/login' => 'login'                
]

动词

OPTIONS
不受支持,要么用
.htaccess
处理,要么需要以不依赖 HTTP 标头的方式更改机制。

© www.soinside.com 2019 - 2024. All rights reserved.