yii 在main.php文件中使用url::to()

yx2lnoni  于 2022-11-09  发布在  PHP
关注(0)|答案(1)|浏览(138)

在我的main.php中我有这个代码

'authClientCollection' => [
            'class' => 'yii\authclient\Collection',
            'clients' => [

                'google' => [
                    'class' => 'Da\User\AuthClient\Google', 
                    'clientId' => 'xxxxxxxxxxxxxxxxxapps.googleusercontent.com',
                    'clientSecret' => 'xxxxxxxxxxxxxxxx',
                    'returnUrl' => Url::to(['user/security/auth', 'authclient' => 'google']], true),
                ],

但我得到这个错误。

Notice: Trying to get property 'controller' of non-object in C:\xampp\htdocs\yii2\vendor\yiisoft\yii2\helpers\BaseUrl.php on line 136

我也试过\Yii::$app->urlManager->createAbsoluteUrl('user/security/auth', ['authclient' => 'google']),
并得到此错误

Notice: Trying to get property 'urlManager' of non-object in C:\xampp\htdocs\yii2\frontend\config\main.php on line 160

我如何在main.php中设置一个绝对网址?谢谢

o2rvlv0m

o2rvlv0m1#

您可以覆盖returnUrlmain.phpon beforeRequest节中的其他组件配置

'components' => [
    'google' => [
        'class' => 'Da\User\AuthClient\Google',
        'clientId' => 'xxxxxxxxxxxxxxxxxapps.googleusercontent.com',
        'clientSecret' => 'xxxxxxxxxxxxxxxx',
    ],
],
'on beforeRequest' => function ($event) {
    Yii::$app->google->returnUrl = Url::to(['user/security/auth', 'authclient' => 'google'], true);
},

参考链接

  • 您还可以处理数据库

相关问题