简体   繁体   English

Symfony2从URL检索参数值

[英]Symfony2 retrieve the parameter values from URL

when i try to get the parameter from url its returning nothing 当我尝试从url获取参数时,它什么也不返回

Here is my url : http://localhost/myapp/web/dailydata/payment/amount/1000 这是我的网址: http://localhost/myapp/web/dailydata/payment/amount/1000

Here is my symfony script : 这是我的symfony脚本:

use Symfony\Component\HttpFoundation\Request;
.
.
public function MyAction()
{
    $request = $this->getRequest();
    $amount = $request->request->get('amount');
    echo 'Amount ='.$amount;
    exit;
}

You need to define a proper route in you routing.yml file like this: 您需要在routing.yml文件中定义一个正确的路由,如下所示:

my_action:
    path:      /dailydata/payment/amount/{amount}
    defaults:  { _controller: <bundle>:<controller>:My }

If you don't specify which part of the route is a parameter you can't access it from the action. 如果您未指定路线的哪一部分是参数,则无法从操作中访问它。 Replace the various gaps (proper URL, bundle and controller) where necessary. 如有必要,替换各种间隙(正确的URL,捆绑和控制器)。

use Symfony\Component\HttpFoundation\Request;
.
.
public function MyAction()
{
    $request = $this->getRequest();
    $amount =$request->attributes->get('amount'); //replace this line in above code
    echo 'Amount ='.$amount;
    exit;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM