我是symfony2 php框架的新手,但我知道它的后端MVC。我正在尝试创建一个存储在数据库中的表单,然后向用户发送一封电子邮件,其中包含他/她在表单中输入的相同信息。为此,我在symfony2框架中使用了doctrine,swiftmailer和entity。 当我使用< form method ='post'action ='saveIndex'>< / form>,它工作得很好但是当我试图实现angularjs的AJAX来发布我的数据时,我得到注意:未定义索引错误。
Html(在树枝模板中):
<form class="productForm">
<div class="inputs" style="float: left">
<input type="text" name="productId" data-ng-model="signup.user.productId"/>
<input type="text" name="firstName" data-ng-model="signup.user.firstName"/>
<input type="text" name="lastName" data-ng-model="signup.user.lastName"/>
<input type="email" name="email" data-ng-model="signup.user.email"/>
<input type="text" name="price" data-ng-model="signup.user.price"/>
<input type="text" name="timeStamp" data-ng-model="signup.user.time"/>
<textarea name="comments" data-ng-model="signup.user.comments"></textarea>
<button type="submit" name="submit" class="btn btn-primary" ng- click="submit(signup.user)">Submit Request</button>
</div>
</div>
</form>
app.js:
myApp.controller('mainController',[
'$scope',
'$location',
'$http',
'$window',
'$rootScope',
function($scope, $location, $http, $window, $rootScope){
$scope.submit = function (user) {
$http({
method: 'POST',
url: 'saveIndex',
data: $scope.signup
})
.success(function(data, status){
$log.warn(data, status); //remove for production
$location.path('success');
})
.error(function(data, status){
$log.warn(data, status); //remove for production
});
}
}
]);
symfony2 controller:
// Save Information
public function saveIndexAction(){
$post = new Post(); // Call to entity named Post
$request = $this->get('request');
$params = $request->request->all();
$session = $this->get('session');
$this->saveIndex(
$params['productId'], <- GETTING UNDEFINED INDEX ERROR
$params['firstName'],
$params['lastName'],
$params['email'],
$params['price'],
$params['timeStamp'],
$params['comments'],
$session
);
$post->setProductId($params['productId']);
$post->setFirstName($params['firstName']);
$post->setLastName($params['lastName']);
$post->setEmail($params['email']);
$post->setPrice($params['price']);
$post->setTimeStamp($params['timeStamp']);
$post->setComments($params['comments']);
// Store information in the database
$em = $this->getDoctrine()->getManager();
$em->persist($post);
$em->flush();
// Send the email
$this->mailAction
(
$post->getId(),
$params['productId'],
$params['firstName'],
$params['lastName'],
$params['email'],
$params['price'],
$params['timeStamp'],
$params['comments']
);
// Return the response
//return new Response('Info Submitted'.$post->getId());
$return=json_encode($post);//json encode the array
return new Response($return,200,array('Content-Type'=>'application/json'));//make sure it has the correct content type
}
我不知道如何使用symfony 2控制器进行AJAX POST并将响应返回Angularjs以执行客户端路由。请任何帮助表示赞赏。谢谢。
答案 0 :(得分:1)
所以,在var_dump($ params)上,我发现输出是数组(size = 0)未定义。
所以,我包含了以下代码:
protected function getRequestJson(){
$params = null;
$content = $this->get("request")->getContent();
if (!empty($content))
{
$params = json_decode($content, true);
}
return $params;
}
public function saveIndexAction(){
$post = new Post();
$signup_params = $this->getRequestJson();
//flatten signup parameters
$params = array();
foreach ($signup_params as $p) {
if (is_array($p)) {
foreach($p as $key=>$val) {
$params[$key]=$val;
}
}
}
}
它有效。