Yii2 $_GET 和 Yii::$app->request->get() 不起作用



背景

我遇到一个奇怪的问题,简单的代码行我不理解。我对LinkedIn Auth使用的控制器有一个操作。用户第一次击中控制器,它将其重定向到LinkedIn站点进行身份验证,一旦用户对重定向链接的用户进行身份验证,将其作为param中的url中的auth代码返回到同一控制器。

http://beta.consynki.com/authentication/network/linkedin?code=DQTdGfxIlbsU...

控制器

class AuthenticationController extends WebController {
    public function actionNetwork($network){
        $access_token = Yii::$app->request->get('code');
        $network_connection      = NetworkFactory::build($network);
        $client = $network_connection->getClient();
        if($access_token && !is_null($access_token)){
            $headers = Yii::$app->response->headers;
            $headers->set('Pragma', 'no-cache');
            $headers->add('X-Access-Token', $access_token);
            return $this->render('success');
        }
        return $this->redirect($client->getLoginUrl(),302)->send();
    }
}

编辑1- webcontroller

/**
 * Class WebController
 *
 * Default controller for public web pages. This class pulls meta tags from a seporately stored file, and makes
 * them available to the view.
 *
 * @package wwwcomponents
 */
class WebController extends Controller {
    public $meta = [];
    public function beforeAction($event) {
        $controller = $this->id;
        $action     = $this->action->id;
        $meta_file  = Yii::getAlias('@www') . '/meta/' . $controller . '/' . $action . '.php';
        if (file_exists($meta_file)) {
            $this->meta = include($meta_file);
            $this->setMetaTags($this->meta);
        }
        return parent::beforeAction($event);
    }
    /**
     * Set the meta tags for a page
     *
     * @param string $type
     * @param        $tag
     * @param        $value
     */
    public function registerMetaTag($type = 'name', $tag, $value) {
        if (!is_null($value)) {
            $this->view->registerMetaTag([
                $type     => $tag,
                'content' => $value
            ]);
        }
    }
    public function behaviors() {
        return [
            /**
             * The particular campaign used.
             *
             * Example social_share, stay_connected_add
             */
            'utm_campaign' => [
                'class'        => 'commonbehaviorTrackingBehavior',
                'queryParam'   => 'utm_campaign',
                'sessionParam' => 'utm_campaign',
            ],
            /*
             * The source of the referral, could be an add network, facebook, email or just a link on the Consynki site.
             *
             * example: google, facebook, citysearch, welcome_email
             */
            'utm_source'   => [
                'class'        => 'commonbehaviorTrackingBehavior',
                'queryParam'   => 'utm_source',
                'sessionParam' => 'utm_source',
            ],
        ];
    }
    protected function setMetaTags($meta) {
        foreach ($meta AS $key => $value) {
            if (is_array($value)) {
                $this->view->registerMetaTag($value, $key);
            }
        }
    }
}

问题

当我尝试从get param Yii::$app->request->get('code');获得 code时,我会得到一个空值。在进一步检查$ _GET数组var_dump($app->request->get()var_dump($_GET);时,我看到code变量的键在其前面具有$ "?code"。这很奇怪。

array(3) { ["network"]=> string(8) "linkedin" ["?code"]=> string(115) "DQTdGfxIlbsU..." ["state"]=> string(32) "12121..." }

研究笔记

看起来yii2修改了$ _get值,它通过URL路由。不确定这是否是问题。已更新到最新版本的yii,但没有解决问题。

问题

为什么会发生这种情况?如何修复它,以便我可以获得code值?

设置这样的规则:

'authentication/network/<network:w+>/<code:w+>' => 'authentication/network',
'authentication/network/<network:w+>' => 'authentication/network',

现在在行动集参数中,例如:

public function actionNetwork($network, $code = null)

您以前的$access_token现在是$code

最新更新