【问题标题】:How can I access to the twig app.request.get('_route_params') in a twig rendered for a forwarded controller?如何访问为转发控制器呈现的树枝中的树枝 app.request.get('_route_params')?
【发布时间】:2016-11-07 16:49:41
【问题描述】:

我正在尝试使用主控制器中的“转发”方法。首先,我向我保证原始的“请求”是从第一个控制器发送到“转发的控制器”(如果我在两个控制器中执行 var_dump,我会得到相同的请求对象。)。但是,当我尝试从转发控制器呈现的树枝访问“请求对象”时,app.request.get(_route)、app.request.get(_route_params) 和其他是不同的。就好像原始请求仅从 twig 全局变量中丢失。

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

当我进入路由 http://localhost/expedia/booking(没有转发)时,我可以正确看到 _route_params 转储。但是如果我使用前向控制器,varDumper 在这两种情况下都会返回 null。

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

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

我做错了什么?

先谢谢你了。

【问题讨论】:

    标签: symfony forward


    【解决方案1】:

    终于解决了我的问题。

    也许它是一个 symfony 错误,因为 rivaros 在 symfony 的问题 https://github.com/symfony/symfony/issues/5804#issuecomment-17331590 中评论了它

    实用的解决方案贴在这里: 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');
       ));
    

    谢谢!

    【讨论】:

      【解决方案2】:

      上述解决方案对我来说效果很好,因为我想从我的自定义模板中访问 URL 路由参数,但 app.request.attributes.get('_route_params') 不起作用。

      我做了一个小改动来在 my_module 文件 (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;
      }
      

      然后您可以通过以下方式访问/转储主题模板文件中的整个请求数据:

      {{ dump(session_request) }}
      

      您可以通过从function my_module_theme(){} 定义中缩小到\Drupal::request() 上的特定数据元素来精细过滤传递给自定义模板的数据。

      另一种选择是从控制器方法传递您需要的变量数据,如下所示:

        /**
         * 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,
          );
        }
      

      然后在你的 .module 文件中注册你的模板文件:

      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;
      }
      

      最后,您可以通过{{ query_params }}{{ dump(route_params) }} 访问模板文件中的这些变量

      谢谢!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-08-07
        • 2022-01-02
        • 1970-01-01
        • 2015-04-22
        • 2021-08-10
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多