1 回答
TA贡献1852条经验 获得超7个赞
我自己想出了解决方案:D。我不知道我的函数中需要 Request,现在也知道它的作用。
javascript 保持不变,控制器现在看起来像这样:
<?php
namespace AppBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Response;
use AppBundle\Entity\User as User;
use Appbundle\Entity\Category as Category;
class HomePageController extends Controller
{
/**
* @Route("/", name="homepage")
*/
public function getData() {
//...
}
/**
* @Route("/create-category", name="newCat")
*/
public function createCategory(Request $request) {
// dump($request);
// dump($request->request->get('output'));
$output = $request->request->get('output');
$category = $this->getDoctrine()
->getRepository('AppBundle:Category')
->findall();
$category = new Category();
$category->setTitle($output);
$em = $this->getDoctrine()->getEntityManager();
$em->persist($category);
$em->flush();
return new Response('Created product id '.$category->getId());
}
}
我更改/添加的内容:
//Added Request $request
public function createCategory(Request $request) {
//Added line for getting the output of ajax/json post.
$output = $request->request->get('output');
}
添加回答
举报