So a user is directed to a page in my ZF2 app as follows:
http://example.com/some/route/variable?auth=1234&channel=1234&tz=1234&locale=&client=1234
The controller loads a form:
if ($prg instanceof Response) {
return $prg
} elseif ($prg === false) {
return new ViewModel([
'form' => $this->transferForm,
]);
}
And in the view:
$form = $this->form;
$form->setAttribute('action', $this->url('some/route',[],true));
$form->setAttribute('method', 'post');
What I want to happen is when the user invokes the form that it posts to the same inbound route, namely:
http://example.com/some/route/variable?auth=1234&channel=1234&tz=1234&locale=&client=1234
Is there a stupidly simple way of doing this?
Or do I need to get each param, and explicityly place them in the form target uri?:
$form->setAttribute('action', $this->url('some/route',[],[query => [''] ]));
As it stands I have written a helper to deal with this:
namespace Some\View\Helper;
use Zend\View\Helper\AbstractHelper;
class ParamsHelper extends AbstractHelper {
public function __invoke()
{
$param1 = (isset($_GET['param1'])) ? $_GET['param1'] : '';
$param2 = (isset($_GET['param2'])) ? $_GET['param2'] : '';
$param3 = (isset($_GET['param3'])) ? $_GET['param3'] : '';
$param4 = (isset($_GET['param4'])) ? $_GET['param4'] : '';
$param5 = (isset($_GET['param5'])) ? $_GET['param5'] : '';
$queryParams = (null == $auth) ? [] : ['query' =>
[
'param1' => $param1 ,
'param2' => $param2 ,
'param3' => $param3 ,
'param4' => $param4,
'param5' => $param5
]
];
return $queryParams;
}
}
This resolves the issue however it still seems a longwinded and not so dynamic way of resolving the issue...
There is a Url
view helper but this helper does (surprisingly) not give you the full request url with query params. But there is also a ServerUrl
helper in ZF2 and this one gives you the full url including query params if used like this:
in case of your example:
http://example.com/some/route/variable?auth=1234&channel=1234&tz=1234&locale=&client=1234
It will do the following:
$viewHelperManager = $serviceManager->get('ViewHelperManager');
$urlHelper = $viewHelper->get('url');
$serverUrlHelper = $viewHelper->get('serverUrl');
$urlHelper(); // --> /some/route/variable
$serverUrlHelper(true); // --> http://example.com/some/route/variable?auth=1234&channel=1234&tz=1234&locale=&client=1234
In your view simply as follows:
$this->url(); // --> /some/route/variable
and
$this->serverUrl(); // --> http://example.com/some/route/variable?auth=1234&channel=1234&tz=1234&locale=&client=1234
Note There are actually more people who complain about not being able to reuse query params in the url view helper. On that page there is also a link to an alternative (refactored) Url
helper class that does reuse query params.