简体   繁体   English

通过AJAX将表单数据作为参数传递给Symfony2控制器功能

[英]Pass form data as parameter to Symfony2 controller function via AJAX

I think this is one of the more complex tricks to get right and therefore I have decided to elicit the help of the very knowledgeable people on StackOverflow. 我认为这是正确的更复杂技巧之一,因此我决定在StackOverflow上寻求知识渊博的人员的帮助。 My scenario is as follows: I have two entities, a user and an account. 我的情况如下:我有两个实体,一个用户和一个帐户。 A user is always linked to an account upon registration (and depending on the type of user, might be linked to more than one account. Upon registration the function saveUser() is called (via ajax from frontend) and the submitted form data is retrieved from the Request Object. This data is then passed to the function saveAccount($data) (which is called in the saveUser() function) in the form of a parameter and the account is created (sometimes called more than once with different data sets to create various accounts), which is linked to the user. 用户总是在注册时链接到一个帐户(并且取决于用户的类型,可能会链接到多个帐户。注册后,将调用函数saveUser()(通过前端的ajax),并检索提交的表单数据然后将数据以参数的形式传递给函数saveAccount($ data)(在saveUser()函数中调用),并创建帐户(有时会使用不同的数据集多次调用)创建各种帐户),该帐户已链接到用户。

Now I want to create an account from my admin panel without creating a user, so I want to call saveAccount($data) directly via ajax (from frontend) and pass the form data to it as a PARAMETER (instead of retrieving it in the function via the Request Object), so that I can use the same saveAccount($data) function and that I do not have to create a saveAccount() which works the the Request variable. 现在,我想从管理面板中创建一个帐户而不创建用户,因此我想直接通过ajax(从前端)调用saveAccount($ data)并将表单数据作为PARAMETER传递给它(而不是在函数通过Request对象),这样我就可以使用相同的saveAccount($ data)函数,而不必创建一个工作于Request变量的saveAccount()。 Does this make sense? 这有意义吗? Is this possible? 这可能吗? If so, how would I go about doing this? 如果是这样,我将如何去做?

I did not post any code, as I did not see the need for it, this is more a conceptional problem, but if you require the code that I have thus far or if anything is unclear I will be happy to elaborate. 我没有发布任何代码,因为我没有看到它的需要,这更多是一个概念上的问题,但是如果您需要我到目前为止所拥有的代码,或者如果不清楚,我将很乐意进行阐述。

There should not be any saveAccount method, you just rely on relationships between entities, ie on a setAccount method, or to a addAccount one in case you need to add an entity to a Collection . 不应该有任何saveAccount方法,你仅仅依靠实体之间的关系,即在setAccount方法,或到addAccount的情况下,一个你需要一个实体添加到Collection

Then Doctrine will take care of saving and persisting everything. 然后,Doctrine将负责保存和保留所有内容。

For saving data, I would always rely on a RESTful interface [which you can create easily via FOSRestBundle ], and send everything via ajax no matter what; 为了保存数据,我将始终依靠RESTful接口[您可以通过FOSRestBundle轻松创建 ],并且无论如何都可以通过ajax发送所有内容; you'll end up with a smoother interface and more maintainable code. 您将获得一个更流畅的界面和更可维护的代码。

For instances where a controller function can be called either via AJAX with form data or internally by a another controller function the following solution works: 对于可以通过带有表单数据的AJAX或另一个控制器内部在内部调用控制器功能的情况,以下解决方案有效:

public function saveAccount($data = null)
{
    if (empty($data)) $data = $this->getRequest()->request->all();
    ...
}

Then you can pass an array to the controller function in the same format as your form data array and it will use that data if passed to the function, otherwise it will retrieve the REQUEST (form) data. 然后,您可以将数组以与表单数据数组相同的格式传递给控制器​​函数,如果传递给函数,它将使用该数据,否则它将检索REQUEST(表单)数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM