0

I'm using ZF3, I know the below works in controllers:

$this->params()->fromQuery('my_get_var', 'default_value'); $this->params()->fromPost('my_post_var', 'default_value'); 

How to get these in views? (of course, without accessing superglobals $_GET and $_POST directly)

2 Answers 2

1

I assume that it is the same as ZF2, passing them to the view.

 $viewModel = new ViewModel(); $viewModel->my_get_var = $this->params()->fromQuery('my_get_var', 'default_value'); $viewModel->my_post_var = $this->params()->fromPost('my_post_var', 'default_value'); return $viewModel; 

You can then display them in your view.phtml

<?php echo $this->my_get_var; ?> <?php echo $this->my_post_var; ?> 

See my answer here.

Sign up to request clarification or add additional context in comments.

1 Comment

I thought maybe there is a different way, but I guess that's how to should be done
0

As an alternative way of returning and accessing you could use the provided "Zend magic".

In a Controller, return as so:

return [ 'getName' => $this->params()->fromQuery('get_var', null), 'postName' => $this->params()->fromPost('post_var', null), ]; 

The magic here is that a ViewModel is automagically created for you. The second piece of magic is that the returned keys are set as variables in the newly created ViewModel.

(edit: just spotted this link provided by @Garry in his answer which already contains the above)

In the ViewModel you could also make use of some of the Zend Framework magic. You could use $getName and $postName instead of $this->getName/$this->postName. So, to print do:

<?= $getName ?: 'No GET params given' ?> <?= $postName ?: 'No POST params given' ?> 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.