开发者

Symfony, get GET parameters in route method

开发者 https://www.devze.com 2023-02-12 15:20 出处:网络
Have a problem. There is my route method: book_list: url:/api/books.:sf_format class:sfDoctrineRoute options:{ model: Book, type: list, method: getActiveWithAuthor }

Have a problem.

There is my route method:

book_list:
  url:      /api/books.:sf_format
  class:    sfDoctrineRoute
  options:  { model: Book, type: list, method: getActiveWithAuthor }
  param:    { module: book, action: list, sf_format: json }
  requirements:
    sf_format: (?:json|html)

code in action is simple:

public function executeList(sfWebRequest $request) {
    $this->books = $this->getRoute()->getObjects();
}

And custom method for getting the Books

public function getActiveWithAuthor(array $parameters) {
    // crit is easy to find in logs.
    sfContext::getInstance()->getLogger()->crit(var_export($parameters, true));
    return BookQuery::create()
    ->addSelf()
    ->addAuthor()
    ->execute();
}

The problem is, that I would like to filter books by optional parammeter "date_from", which could be in url, e.g. /api/books?date_from=2011-02-18

But in log I could see on开发者_如何学Goly "sf_format => html" (or json) What should I use for getting the optional parrameter "date_from"?


public function executeList(sfWebRequest $request) 
{
  $this->books = Doctrine::getTable('Book')-> getActiveWithAuthor($request->getParameter('date'));
}

//BookTable.class.php
public function getActiveWithAuthor($date) 
{
  $q = $this->createQuery('b')
    ->leftJoin('b.Author')
    ->where('b.date > ?', $date);

  return $q->execute();  
}


You can get your parameter from the request object:

sfContext::getInstance()->getRequest()->getParameter('date_from');

UPDATE Better solution, without sfContext::getInstance() :

class myCustomRoute extends sfDoctrineRoute
{
  public function getRealVariables()
  {
    return array_merge('date_from', parent::getRealVariables());
  }
}

Specify the use of this class in routing.yml and you may use this parameter directly in your method.

0

精彩评论

暂无评论...
验证码 换一张
取 消