0

I have in my twig a link as shown below and I would like to send a lot of data (array) to a controller. How do I go about it ? Do I use params? what is the best way to handle this in your opinion? Thanks.

In my twig:

 

 <a href="{{ path('mymodule.send') }}">Send</a>

Routing file:

mymodule.send:
  path: '/admin/send'
      defaults:
        _controller: '\Drupal\mymodule\Controller\MyController::myFunction'
        _title: 'Send data'
      requirements:
        _permission: 'administer site configuration'
      options:
        _admin_route: TRUE

My controller where I want to send data:

class MyController extends ControllerBase {

      public function myFunction() {
        }
   
    }
2
  • 1
    Is the template and the controller on the same Drupal? Then I wouldn't send the data via the request, only a reference to the data in a cookie. The easiest way to implement this is setting the array as session value and removing it after the controller has fetched the data, everything else is done by Drupal/PHP.
    – 4uk4
    Jun 17 at 6:43
  • What is the data that you want to pass? it is entity field values? One way to do it is to replace the link with a config form with an action button and embed it in the twig template, the form can collect the data that you want and redirect it to the controller which will give you more control of the data. If you can share more information I might be able to provide a more accurate answer. Jun 17 at 11:07

1 Answer 1

0

A lot of data should typically be in the body of an HTTP message, so a POST with data as JSON, application/x-www-form-urlencoded, or multipart/form-data is the way.

If there is not so much data, and it is a one-dimension array, and it is acceptable that it will be logged in webserver access logs, you can use query strings.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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