Yii2 $ _GET和Yii :: $ app-> request-> get()无效

时间:2016-12-08 12:33:20

标签: php yii get yii2

背景

我遇到一个奇怪的问题,我不明白一行简单的代码。我在控制器上有一个动作,我正在使用LinkedIn认证。用户第一次点击控制器时,它会重定向到LinkedIn网站进行身份验证,一旦用户验证链接重定向回到同一个控制器,其中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 www\components
 */
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'        => 'common\behavior\TrackingBehavior',
                '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'        => 'common\behavior\TrackingBehavior',
                '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参数code获取Yii::$app->request->get('code');时,我得到一个NULL值。在进一步检查$ _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值?

1 个答案:

答案 0 :(得分:1)

像这样设置规则:

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

现在在行动中设置参数,如:

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

您之前的$access_token现在是$code