laminas / laminas-mvc

Laminas's event-driven MVC layer, including MVC Applications, Controllers, and Plugins
https://docs.laminas.dev/laminas-mvc/
BSD 3-Clause "New" or "Revised" License
139 stars 51 forks source link

LazyControllerAbstractFactory doesn't look in FormManager #17

Open weierophinney opened 4 years ago

weierophinney commented 4 years ago

The extremely convenient LazyControllerAbstractFactory only checks ServiceManager when making objects for the controller's constructor.

This means that I have to move any forms without a factory from the 'form_elements' config array to 'service_manager'... and odd delineation.


Originally posted by @bitwombat at https://github.com/zendframework/zend-mvc/issues/255

froschdesign commented 3 years ago

The form element manager is registered with the class name Laminas\Form\FormElementManager in laminas-form and this can be used via the registered module in a laminas-mvc based application.

Example

class ExampleController extends Laminas\Mvc\Controller\AbstractActionController
{
    public function __construct(
        FilterPluginManager $filterPluginManager,
        FormElementManager $formElementManager,
        HydratorPluginManager $hydratorPluginManager,
        InputFilterPluginManager $inputFilterPluginManager,
        ValidatorPluginManager $validatorPluginManager
    ) {
        $this->filterPluginManager      = $filterPluginManager;
        $this->formElementManager       = $formElementManager;
        $this->hydratorPluginManager    = $hydratorPluginManager;
        $this->inputFilterPluginManager = $inputFilterPluginManager;
        $this->validatorPluginManager   = $validatorPluginManager;
    }
}
'controllers' => [
    'factories' => [
        ExampleController::class  => Laminas\Mvc\Controller\LazyControllerAbstractFactory::class,
    ],
],

Reflection Factory of laminas-servicemanager

If the following features are not needed in a controller constructor:

…then the laminas-servicemanager's reflection factory can be used:

'controllers'     => [
    'factories' => [
        ExampleController::class  => Laminas\ServiceManager\AbstractFactory\ReflectionBasedAbstractFactory::class,
    ],
],

Conclusion

All aliases can be removed from the lazy controller factory:

https://github.com/laminas/laminas-mvc/blob/150e0b44f9bc814619163ea5c68a71cc2d6ee724/src/Controller/LazyControllerAbstractFactory.php#L86-L97