iモードIDを楽に取得する方法


昨日からDoCoMoの端末からiモードIDというものが取得できるようになりました。


取得する方法は、URLに「guid=ON」というクエリーを付加して、

そのURLを踏んで訪れたリクエストから$_SERVER['HTTP_X_DCMGUID']を取得するだけ。

「だけ」なんですが、サイト上の全てのaタグに付加するのはなかなか骨が折れます。


symfonyで、link_to()やurl_for(),redirect()などでURLの出力を実装している場合は、

必ずgenUrl()メソッドを経由しているので、このメソッドをオーバーライドしてあげて

ちょこちょこっと細工をすると楽ができます。

  1. apps/your_application/config/factories.yml に以下を記述


    all:
    controller:
    class: myFrontWebController
  2. apps/your_application/lib/myFrontWebController.class.php を作成


    class myFrontWebController extends sfFrontWebController
    {
    /**
    * genUrlをオーバーライド
    */
    public function genUrl($parameters=array(), $absolute=false)
    {
    $anchor = '';
    $match = array();
    $url = parent::genUrl($parameters, $absolute);

    //事前にPEAR::Net_UserAgent_Mobileなどを使ってキャリアを取得しておく。
    $career = $this->getContext()->getRequest()->getAttribute('mobile_career');
    $query_strings = array();

    if(preg_match('/^(.+?)#(.+?)$/', $url, $match)){
    $url = $match[1];
    $anchor = $match[2];
    }

    if (defined('SID') && SID != ''){
    $query_strings[] = SID;
    }

    //iモードID対応
    if($career == 'docomo'){
    $query_strings[] = 'guid=ON';
    }

    $url .= '?' . implode('&',$query_strings);

    if ($anchor) {
    $url .= '#' . $anchor;
    }

    return $url;
    }
    }

  3. お決まりの


    symfony cc

sekine