Symfony get get parameters in route method

You 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) 

The code in action is simple:

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

And a custom method for getting 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 with the optional parameter "date_from", which can be in the url, for example. / API / books? Date_from = 2011-02-18

But in the log I could only see "sf_format => html" (or json) What should I use to get the optional parameter "date_from"?

+4
source share
2 answers
 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(); } 
+1
source

You can get your parameter from the request object:

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

UPDATE Best solution without sfContext :: getInstance ():

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

Indicate the use of this class in routing.yml, and you can use this parameter directly in your method.

+1
source

Source: https://habr.com/ru/post/1340640/


All Articles