0
votes

I'm trying to use the "forward" method from a main Controller. first I'm assuring me that the original "request" is sent from the first controller to the "forwarded controller" (If I do a var_dump in both controllers I get the same request object.). But when I try access to the "request object" from the twig rendered from forwarded controller, the app.request.get(_route), app.request.get(_route_params) and others are differents. It is as if the original request is lost only from twig globals.

namespace MyBundle\Controller;

//...    

class BookingController extends Controller {

    /**
     * @Route("/book/hotel/{slug}", name="booking_hotel")
     */
    public function establishmentAction(Request $request, $slug = null) {
        \dump($request); // dumper 1

        return $this->forward('ExpediaBundle:Booking:index', array('request' => $request);
    }
}
namespace ExpediaBundle\Controller;

//...

class BookingController extends Controller {

    /**
     * @Route("/expedia/booking", name="expedia_booking")
     * @Template()
     */
    public function indexAction(Request $request)
    {
        \dump($request); // dumper 2

        // The dumper 1 === dumper 2

        return array();
    }
}
{# file: ExpediaBundle:Booking:index.html.twig  #}    

{{ dump(app.request.attributes.get('_route_params')) }} -> works fine
{{ dump(app.request.get('_route_params')) }} -> works fine

When I enter to the route http://localhost/expedia/booking (without the forward) I can see the _route_params dumps correctly. But if I use the forward controller the varDumper returns null for both cases.

http://localhost/book/hotel/hotelname:

{{ dump(app.request.attributes.get('_route_params')) }} -> null
{{ dump(app.request.get('_route_params')) }} -> null

What am I doing wrong?

Thank you in advanced.

2

2 Answers

1
votes

Finally, I was solve my problem.

Maybe its a symfony bug as it is commented by rivaros in the symfony's issues https://github.com/symfony/symfony/issues/5804#issuecomment-17331590

The practical solution is posted here: https://github.com/symfony/symfony/issues/5804#issuecomment-17379777

return $this->forward('Yourbundle:Controller:action',
   array(
      //... your parameters...,
      '_route' => $this->getRequest()->attributes->get('_route'),
      '_route_params' => $this->getRequest()->attributes->get('_route_params');
   ));

Thank you!

0
votes

The above solution worked well for me since I wanted to access the URL route parameters from my custom template but app.request.attributes.get('_route_params') was not working.

I did a minor change to implement it on my_module file (Drupal-8):

function my_module_theme(){

  $themes['my_module_template_name'] = [
    'template' => 'my-module-template-name',
    'variables' => [
      // define variable(s) you want available on your template
      'session_request' => \Drupal::request(),
  ];

  return $themes;
}

Then you can access/dump the whole request data on your theme template file by:

{{ dump(session_request) }}

You can fine filter out the data you pass to your custom template by narrowing down to a particular data element on the \Drupal::request() from within the function my_module_theme(){} definition.

Another option is to pass the variables data that you need from a controller method like so:

  /**
   * Method to call your custom template
   * You can call this method from your routes
   */
  public function MyMethod() {

    $query_params = \Drupal::request()->query->get('name');
    $route_params = \Drupal::request()->attributes->get('_route_params')['name_of_parameter'];

    return array(
      '#theme' => 'my_module_template_name',
      '#query_params' => $query_params,
      '#route_params' => $route_params,
    );
  }

Then on your .module file you register your template file:

function my_module_theme(){

  $themes['my_module_template_name'] = [
    'template' => 'my-module-template-name',
    'variables' => [ // define the variable(s)
      'query_params' => '',
      'route_params' => '',
  ];

  return $themes;
}

Finally, you can access these variables on your template file by: {{ query_params }} or {{ dump(route_params) }}

Thank You!