Symfony2路由中实现了多个参数

时间:2013-11-25 21:48:31

标签: php symfony routing

这是正常的GET请求网址

http://localhost/person.php?name='Jack'&age=25&gender='male'

如何将其实现到Symfony2路由?


经过测试,这是我的解决方案:

acme_person_info:
    pattern:  /person/name/{name}/age/{age}/gender/{gender}
    defaults: { _controller: AcmeUserBundle:Person:info }

class PersonController extends Controller
{
    public function infoAction($name, $age, $gender) 
    {
             // do something here
    }

}

2 个答案:

答案 0 :(得分:3)

无论如何你应该阅读symfony doc。见http://symfony.com/doc/current/book/routing.html

// app/config/routing.yml
person:
    path:      /person/{name}-{age}-{gender}
    defaults:  { _controller: AcmeBlogBundle:Person:index }

// src/Acme/BlogBundle/Controller/PersonController.php
namespace Acme\BlogBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class PersonController extends Controller
{
    public function indexAction($name, $age, $gender)
    {
        // do something
    }
}

答案 1 :(得分:2)

您可以在Request对象中访问查询参数。

$request->query->get('query_parameter_name_here');

例如,

use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class PersonController extends Controller
{
    public function indexAction(Request $request)
    {
        // retrieve query parameter with $request
        $person = $request->query->get('name');
        $age = $request->query->get('age');
    }
}

请注意,

对于GET / person,您可以访问query属性

$request->query

对于POST / person,您可以访问request属性

$request->request