Symfony 2 转发请求传递 GET/POST 参数 [英] Symfony 2 Forward Request passing along GET/POST params

查看:20
本文介绍了Symfony 2 转发请求传递 GET/POST 参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以转发请求,传递所有 GET/POST 参数?

Is it possible to forward a request, passing along all GET/POST params?

我想如果我只是这样做

$this->forward('dest')

我会在没有任何 GET/POST 参数的情况下转到 dest 吗?

I will go to dest without any GET/POST params?

更新

我的目标实际上是拥有一个像 addSomething 这样的控制器操作,它检查用户是否有足够的项目"来添加一些东西.然后将请求转发给合适的控制器,继续实际添加adding{Type}Something

My objective is actually to have a controller action like addSomething that takes checks that the user has the sufficient "items" to add something. Then forward the request to the approperiate controller to continue the actual adding of adding{Type}Something

或者是否会在所有控制器中获得检查"服务以进行检查更合适?无论如何,我认为知道如何使用所有参数转发到控制器操作是很有用的

Or would getting a "checking" service in all controllers that does the checking be more appropriate? Anyways, I think its informative to know how to forward to a controller action with all params

推荐答案

我在这里看不出有任何理由将请求转发回内核.您可以按照您的建议,将这个逻辑封装在检查器服务中,或者您可以创建一个 kernel.request 侦听器,该侦听器在路由器侦听器之后运行并应用 _controller 属性,仅当您的条件满足时.

I don't see any reason here to forward the request back through the kernel. You can go the route of encapsulating this logic in a checker service, as you've suggested, or you may be able to create a kernel.request listener that runs after the router listener and applies the _controller attribute only if your conditions are met.

例如这个routing.yml:

some_route:
    pattern:  /xyz
    defaults: { _controller_candidate: "FooBundle:Bar:baz" }

还有这个听众:

class MyListener
{
    public function onKernelRequest($event)
    {
        $request = $event->getRequest();
        if (!$controller = $request->attributes->get('_controller_candidiate')) {
            return;
        }

        if (/* your logic... */) {
            $request->attributes->set('_controller', $controller');
        }
    }
}

配置为核心路由器侦听器之后运行:

Configured to run after the core router listener:

services:
    my_listener:
        class: MyListener
        tags:
            -
                name:     kernel.event_listener
                event:    kernel.request
                priority: -10

核心路由器侦听器的优先级在 Symfony 2.0 中为 0,在 Symfony 2.1 中为 32.在任何一种情况下,-10 的优先级都应该起作用.

The priority of the core router listener is 0 in Symfony 2.0 and 32 in Symfony 2.1. In either case, a priority of -10 should work.

我很好奇这是否有效:)

I'm curious to see if this works :)

这篇关于Symfony 2 转发请求传递 GET/POST 参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆