I have a form which triggers this method through the .routing.yml file.

public function getTrainerLessonsData(){
    $request = \Drupal::request();
    $trainerId = $request->get('trainer');
    $place = $request->get('place');
    $startDate = $request->get('startDate');
    $endDate = $request->get('endDate');
    $dao = \Drupal::service('trainerlessons.lessons_dao');
    if(empty($startDate) || empty($endDate)){
        var_dump('dates required');
    $newStartDate = $this->formatDate($startDate);
    $newEndDate = $this->formatDate($endDate);
    if(!empty($trainerId) && !empty($place)){
        var_dump('does not work');
    } else if(!empty($trainerId)){
        $result = $dao->getTrainerLessonsFromTrainerId($request->get('trainer'), $newStartDate, $newEndDate);
    } else if(!empty($place)){
        $result = $this->getTrainersByPlace($place, $newStartDate, $newEndDate);
    // return $this->redirect('trainerlessons.trainerlessonsinfo', $result);
    return $result;

The method searches for some data and returns an array filled with small objects. Until this part all my code works fine.

I was wondering if it's possible to send this array filled with objects with a redirect. So that I can use the data on a different page.

I already tried the redirect method, which is commented out in my code. But that gives this error warning: rawurlencode() expects parameter 1 to be string, object given in

  • No, it's not possible. Either the server sends data or a redirect.
    – apaderno
    Jul 18 at 9:59
  • @apaderno Are there any other ways to pass over the data? I need to send the data back to the twig view, which also generates the form. And the currently setup I have is, having 2 routes. One which generates the form view. And the other one that handles the form data.
    – Jorn Reed
    Jul 18 at 10:01
  • You'll either need to stash it in the session/user temp store and pick it up in the next request, or (less good IMO) tack the IDs of the objects onto the URL, and pick those up in the next request instead
    – Clive
    Jul 18 at 11:17
  • @JornReed It's quite hard to answer that without knowing in which way the shown code is related to that view. You could store that data in cache, the session, or using other services, redirect the users back to the view, which should read that data from the same store.
    – apaderno
    Jul 18 at 11:27
  • @apaderno I managed to fix it kinda differently. I put my twig render functionality in a new private method. So that I can re-use it. When you submit the form, it's action leads you to another route, that gets some data from the db, and renders that same twig file with data again.
    – Jorn Reed
    Jul 18 at 11:44


Your Answer

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

Browse other questions tagged or ask your own question.