doudou_3636 2013-07-24 11:11
浏览 51
已采纳

如何在onBeginRequest中获取URL命名参数?

Is there a way to access the rewritten $_GET variables in my onBeginRequest event handler when using a parameterized route?

My route is defined as:

'<_c:\w+>/<_a:\w+>/<id:\d+>' => '<_c>/<_a>'

And my event handler is:

'onBeginRequest' => function($event) {
    /*
    site.com/posts/edit/1 - empty (bad)
    site.com/posts/edit/?id=1 - not empty(good)         
    */
    var_dump($_GET);
    die;
}

How do I make sure that $_GET['id'] is defined no matter which of the two URLs above is requested?

I am using Yii version 1.1.13.

  • 写回答

1条回答 默认 最新

  • douzhi2760 2013-07-24 11:19
    关注

    You do this by having the url manager component parse the current request url:

    $app = Yii::app();
    $app->getUrlManager()->parseUrl($app->getRequest());
    

    This will populate $_GET and $_REQUEST appropriately according to your route. It will also return the active route (controller/action pair), but I am not doing anything with the return value because it looks like you don't need it here.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试