2013-11-25 2 views
0

Вот нормальный GET запрос URLНесколько параметров реализует в Symfony2 маршрутизации

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

Вы должны прочитать Symfony документ в любом случае. см. 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

Вы можете получить доступ к параметру запроса в объекте Запрос.

$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/человека, вы получаете доступ к query атрибут

$request->query 

Для POST/человека, вы получаете доступ к request атрибут

$request->request 
Смежные вопросы