Passing controller variable to form

I have done some forms based on fieldsets in my project. Now when calling my form I would like to pass the controller variable and use it in my form for other purposes. Or in my form factory, how can I access the route parameter retrieved in my controller ?

I might be wrong, but why not pass parameters to the form’s constructor by controller?

I call my form via factories. this is why I cannot pass parameters to the form’s constructor in my controller

Do not break the borders, i.e. do not try to get the request outside of controllers or request handlers.

Please check if a setter method is an option for your use case.

A very late reply. May it help someone. It is not the best solution but can be treated as a workaround to get the task done.

  1. Factories invoke method has a container interface as a first parameter that can access the request object at will. So, you can use that. I’ve not used this before, therefore can’t help you what will be the exact code for it. (Note: This suggestion may not work in future Laminas MVC version above 3.3.*)
  2. You can have your factory return a basic form with a basic fieldset and add the desired fieldset after passing the parameter to the form object.


This collides with my previous message:

Hi, to get the work done at first. You can break and fix it afterwards. Sometimes we’ve to break the rules to meet the needs. When we become perfect or have enough command to see and make things it should be. Like not everyone can think like Ocraimus, Evan, MWOP etc. Not everyone has access to them as a mentor. So, someone has to start somewhere. Thanks!

Unfortunately you have misunderstood something because there is no guarantee that the request object will also be available via the container in the future. That is why I refer to the borders.

That is true. But you’re forgetting something as well. There are projects which run on PHP 5.6 and projects which still run on ZF1 or even ZF2. So, the future becomes subjective based on the project. We live in a system where once a system is developed people keep on running it until it can run. The biggest example is WordPress.

WordPress from some is the worst thing in the development market due to its workflow. But for many, it is the best thing. So, a requirement changes according to the need. Many will not even understand what you’re talking about. An example can be given by a post in this very forum where Ocraimus and some other person argue about Doctrine and Active records.

So to end my reply, what you’ve said is true then can you let me know the date so that I can edit my answer and write it in bold that this answer will not work after 2023 or so on? Thanks!

Don’t worry, I also maintain similar applications. :smiley:

Passing a variable to a form works as follows:

namespace Application\Controller;

use Application\Form\ExampleForm;
use Laminas\Form\FormElementManager;
use Laminas\Mvc\Controller\AbstractActionController;

class IndexController extends AbstractActionController
    private FormElementManager $formElementManager;

    public function __construct(FormElementManager $formElementManager)
        $this->formElementManager = $formElementManager;

    public function indexAction(): array
        /** @var ExampleForm $form */
        $form = $this->formElementManager->build(
                'my_param' => $this->params()->fromRoute('my_param'),

        return ['form' => $form];

This works also in older versions of zend-servicemanager und zend-form.

1 Like

Thanks for sharing this information. This is new information for me. I’ve never used build to create a form. Just recently used it in conjunction with doctrine. But, I’m facing a problem which I’ll ask you in a separate question if I’m not able to solve it by this weekend. Thanks!