Routing Phalcon:如何使用路由在url中获取参数

Routing Phalcon:如何使用路由在url中获取参数,routing,phalcon,Routing,Phalcon,我想获得URL的“q”参数值,如下所示: http://api.domain.com/artist?callback=jQuery1710976531726308167_1400000891029&q=thanh&_=1400000895743 $app->get("/artist", function () { $request = new Phalcon\Http\Request(); $q = $request->get('q'); e

我想获得URL的“q”参数值,如下所示:

http://api.domain.com/artist?callback=jQuery1710976531726308167_1400000891029&q=thanh&_=1400000895743
$app->get("/artist", function () {
    $request = new Phalcon\Http\Request();
    $q = $request->get('q');
    echo $q;
});
如何使用Phalcon中的路由实现这一点?我尝试了这个,但不匹配:

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


在Phalcon中,使用Phalcon\Http\Request对象检索查询字符串参数。如果您试图为Phalcon的微框架定义路由,则应按以下方式定义路由:

http://api.domain.com/artist?callback=jQuery1710976531726308167_1400000891029&q=thanh&_=1400000895743
$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;
});

您可以通过以下两种方式完成:

http://api.domain.com/artist?callback=jQuery1710976531726308167_1400000891029&q=thanh&_=1400000895743
$app->get("/artist", function () {
    $request = new Phalcon\Http\Request();
    $q = $request->get('q');
    echo $q;
});
方式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");

希望这可以正常工作。

路由不能包含查询参数,只需要$app->get(“/artist”),太好了!成功了。谢谢你,twistedtra。你可以用这个,$this->request->get('artist');