4

What's the key difference between:

amazing_forms_contribute:
  path: 'amazing-forms/contribute'
  defaults:
    **_form: '\Drupal\amazing_forms\Form\ContributeForm'**
    _title: 'Conribute page'
  requirements:
    _permission: 'access content'

and

amazing_forms_contribute:
  path: 'amazing-forms/contribute'
  defaults:
    _**controller: '\Drupal\amazing_forms\Controller\ContributeForm::content'
link_id: 'arm.admin_product_variants_create'**
    _title: 'Conribute page'
  requirements:
    _permission: 'access content'

When I should use a controller? And when I should use a form?

Can I combine both (use controller and form)?

What is their limitation?

1 Answer 1

4

_form: We use this explicitly when one expects a classname implementing FormInterface.

_controller:It is used when one needs to return a renderable array or an object. Hence, here we call a method from its class (classname::method)

And NO you cannot use _form and _controller at the same time under defaults: since one routing only provides one functionality at a time.

See here to understand how the structure of routes are used and why they were used.Also see routing api for a more detailed understanding.

3
  • so, can i say, form is to make a form? and _controller is just to present a content?
    – AnD
    May 15, 2016 at 15:11
  • yes _form is to make a form and _controller is not only to present the method (in ur case content) its used to return renderable arrays and also response objects..should i add some links to the answer 2 make it clearer ?? @AnD May 15, 2016 at 15:16
  • There is always exactly one controller. _form, _entity_form, _entity_view and so on are just short cuts that are mapped to a generic (entity) form controller.
    – Berdir
    May 15, 2016 at 20:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.