Is it possible to route request to controllers/actions by query string parameters? And how to do it using routing.yml file?
I mean:
/some/path?do=action1...
some_route:
url: /some/path
param: { module: module1, action: action1 }
requirements:
do: action1 <--- ???
/some/path?do=action2
some_route2
url: /some/path
param: { module: module1, action: action2 }
requirements:
do: action2 <--- ???
or some common route:
some_route:
url: /some/path?do=:action
param: { module: module1, action: action }
requirements:
do: action\d+
Thanks!
Yes, it is possible to do so by creating generic rule in routing.yml
:
generic_rule:
url: /some/path/:action
param: {module: yourmodule}
In this case when you call /some/path/index
then indexAction
method of yourmodule
will be called.
You can even create more generic rule, where both action
and module
are variables:
more_generic_rule:
url: /some/path/:module/:action
Check doc for more info.
You can make one action which will check the do
parameter and forward you to the proper action, e.g.
main_action:
url: /some/path/:do
param: { module: yourmodule, action: mainAction }
requirements:
do: action\d+
Then in the yourmodule/actions/actions.class.php
:
public function executeMainAction(sfWebRequest $request)
{
$do = $request->getParameter('do');
if (doParameterIsOk($do)) {
$this->forward('yourmodule', $do);
} else {
// Handle bad `do` parameter.
}
}