【问题标题】:Phalcon: How to get parameter in url using routingPhalcon:如何使用路由获取 url 中的参数
【发布时间】:2014-07-01 14:10:48
【问题描述】:

我想得到这样的url的“q”参数值:

http://api.domain.com/artist?callback=jQuery1710976531726308167_1400000891029&q=thanh&_=1400000895743

如何在 Phalcon 中使用路由来做到这一点?我试试这个但不匹配:

$app->get("/artist?callback={callback:(.*)}&q={q:(.*)}&_={_:(.*)}", function ($q) {
    //my logic code
}

$app->get("/artist?callback=jQuery1710976531726308167_1400000891029&q=thanh&_=1400000895743", function () {
    //my logic code
}

【问题讨论】:

  • 路由不能包含查询参数,只需要$app->get("/artist")。
  • 太棒了!有效。谢谢你,twistedtra。
  • 你可以用这个,$this->request->get('artist');

标签: routing phalcon


【解决方案1】:

您可以通过两种方式进行操作,如下所示:

方式一:使用获取参数

$this->request->get('artist');

方式2:使用路由器

在路由器中:

$router->add("/artist/id/([a-zA-Z0-9\_\-])", array( 'controller' => 'index', 'action' => 'artist', 'name' => 'abc' ));

在控制器中:

$id = $this->dispatcher->getParam("name");

希望一切顺利。

【讨论】:

    【解决方案2】:

    在 Phalcon 中,您使用 Phalcon\Http\Request 对象来检索您的查询字符串参数。如果你想为 Phalcon 的 Micro Framework 定义路由,你的路由应该这样定义:

    $app->get("/artist", function () {
        $request = new Phalcon\Http\Request();
        $q = $request->get('q');
        echo $q;
    });
    

    您没有使用查询字符串参数定义路由。您的路线将根据 url 中问号之前的部分与上述内容匹配。此外,将参数传递给处理 get 请求的匿名函数是针对 URL 模式中的 REST 参数。如:

    $app->get("/artist/{name}", function ($name) {
        echo $name;
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-02-09
      • 2013-02-19
      • 2015-11-03
      • 2016-11-02
      • 2017-07-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多